开发者

Select every element that is not inside another element

开发者 https://www.devze.com 2023-01-26 19:18 出处:网络
<img src=\"xxx\" alt=\"xxx\" title=\"xxx\"> <div class=\"buttons\"> <a href=\"xxx\"><img src=\"xxx\" alt=\"xxx\" title=\"xxx\"></a>
<img src="xxx" alt="xxx" title="xxx">
<div class="buttons">
    <a href="xxx"><img src="xxx" alt="xxx" title="xxx"></a>
</div>

I need to write a jQuery selector, that will select ONLY images with title attributes 开发者_高级运维that are OUTSIDE the .buttons div. I know that to select images with title attributes I need to use something like this:

$("img[title]")

I know that there is something like :not() selector in jQuery, but I can't find the way to combine them together to achieve that exact result.


You can get the result set then filter it using .not(), like this:

$("img[title]").not(".buttons img")

Or filter in the same selector using :not() (but this is probably a bit slower in older browsers), like this:

$("img[title]:not(.buttons img)")
0

精彩评论

暂无评论...
验证码 换一张
取 消