如何在过滤时让jquery忽略id(How do I get jquery to ignore ids when filtering)

编程入门 行业动态 更新时间:2024-10-12 18:23:07
如何在过滤时让jquery忽略id(How do I get jquery to ignore ids when filtering)

我在内容div中搜索所有id并在href之前进行搜索。 我无法预测儿童的ids会是什么,所以它无论如何都要预先设定。

如何让jquery搜索dom但忽略cetain div的id和它的孩子?

$('*[id]').each(function() { var selectVariable = this; $(selectVariable).prepend('<a href="#" class="" id="' + $(selectVariable).attr('id') + '">*</a>'); }); <div id='content'> <!-- How do I stop the jquery from prepending `id='I can predict'` and all its children--> <div id='I can predict'> <div id='I can't predict'> </div> <div id='I can't predict'> </div> </div> <div id='another piece of content'> <div id='I can't predict (but needs an a link)'> </div> </div> </div>

I am searching all ids in a content div and prepending a href. I can't predict what the child ids will be so it prepends it anyway.

How do I get the jquery to search the dom but ignore cetain div's id and its children?

$('*[id]').each(function() { var selectVariable = this; $(selectVariable).prepend('<a href="#" class="" id="' + $(selectVariable).attr('id') + '">*</a>'); }); <div id='content'> <!-- How do I stop the jquery from prepending `id='I can predict'` and all its children--> <div id='I can predict'> <div id='I can't predict'> </div> <div id='I can't predict'> </div> </div> <div id='another piece of content'> <div id='I can't predict (but needs an a link)'> </div> </div> </div>

最满意答案

您可以使用.not($('#I_can_predict, #I_can_predict *'))来排除您想要的部分:

$('*[id]').not($('#I_can_predict, #I_can_predict *')).each(function() {
    var selectVariable = this;
    $(selectVariable).prepend('<a href="#" class="" id="' 
                              + $(selectVariable).attr('id') + '">*</a>');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='content'>
    content
     <!-- How do I stop the jquery from prepending  `id='I can predict'` and all its children-->
    <div id='I_can_predict'>I can predict
         <div id="I_can't_predict">I can't predict1
         </div>

          <div id="I_can't_predict2">I can't predict2
          </div>
    </div>

    <div id='another_piece_of_content'>another piece of content
          <div id="I_can't_predict_(but_needs_an_a_link)">I_can't_predict_(but_needs_an_a_link)
          </div>
    </div>
</div> 
  
 

You can use .not($('#I_can_predict, #I_can_predict *')) to exclude the part you want out:

$('*[id]').not($('#I_can_predict, #I_can_predict *')).each(function() {
    var selectVariable = this;
    $(selectVariable).prepend('<a href="#" class="" id="' 
                              + $(selectVariable).attr('id') + '">*</a>');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='content'>
    content
     <!-- How do I stop the jquery from prepending  `id='I can predict'` and all its children-->
    <div id='I_can_predict'>I can predict
         <div id="I_can't_predict">I can't predict1
         </div>

          <div id="I_can't_predict2">I can't predict2
          </div>
    </div>

    <div id='another_piece_of_content'>another piece of content
          <div id="I_can't_predict_(but_needs_an_a_link)">I_can't_predict_(but_needs_an_a_link)
          </div>
    </div>
</div> 
  
 

更多推荐

本文发布于:2023-07-15 07:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111402.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   jquery   id   filtering   ids

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!