简体   繁体   English

jQuery-查找包括自我

[英]jQuery - find including self

This gives empty output, because console.log is never called. 这将提供空的输出,因为从不调用console.log。 How can i force jQuery to find on all elements, even top level elements? 我如何强制jQuery在所有元素上找到,甚至在顶级元素上?

    $("<div attr></div><div></div>")
       .andSelf()
       .find('[attr]')
       .each(function (index, el) {console.log(el);});

You can use find to find the descendant elements with the said properties using .find('[attr]') then add back the root elements matching the filter using .addBack('[attr]') 可以使用发现使用以找到具有所述性质的后代元素.find('[attr]')然后加回匹配使用过滤器的根元素.addBack('[attr]')

$("<div attr></div><div></div>")
    .find('[attr]')
    .addBack('[attr]')
    .each(function (index, el) {
    console.log(el);
});

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM