如何在过滤时让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>
更多推荐
发布评论