我有以下布局:
<div class="container">
<p> target </p>
<p> target </p>
<div>
<p> not target </p>
</div>
</div>
我的问题是,如何定位所有顶级 <p>
标签(参见 target
)使用 .find()
?
我可以定位所有 <p>
标签:
jQuery(this).find("p"); // 'this' is .container
我可以针对第一个 <p>
标签:
jQuery(this).find("p").first();
但我想同时定位到顶级 <p>
.container
中的标签.
使用 CSS,我可以通过以下方式做到这一点:.container > p
最佳答案
您可以使用 CSS 直接子选择器:
jQuery(this).find("> p");
或者你可以使用.children()
jQuery(this).children("p");
关于javascript - .find() 顶层 <p> 标签在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36915307/