admin管理员组文章数量:1022944
I'm trying to select an element through jQuery, but one of the element in the selection is optional. So I have something like this:
$('div.myClass span a>img')
In my scenario, that a
element is an optional one. It may or may not exist in the DOM.
Of course, I can write out the entire line again with one containing a
but this looks verbose:
$('div.myClass span a>img').css('opacity', 0.5);
$('div.myClass span img').css('opacity', 0.5);
Is it possible to define the a
element to be optional in the jQuery selector path?
I'm trying to select an element through jQuery, but one of the element in the selection is optional. So I have something like this:
$('div.myClass span a>img')
In my scenario, that a
element is an optional one. It may or may not exist in the DOM.
Of course, I can write out the entire line again with one containing a
but this looks verbose:
$('div.myClass span a>img').css('opacity', 0.5);
$('div.myClass span img').css('opacity', 0.5);
Is it possible to define the a
element to be optional in the jQuery selector path?
5 Answers
Reset to default 5You only need the a >
if you want to specifically target only images within the anchor. Otherwise, div.myClass span img
will target any image inside the span.... regardless of the presence of an anchor tag.
$('div.myClass span a > img');
<div class="myClass">
<span>
<a href="#"><img src="THIS IMAGE IS TARGETED" /></a>
<img src="BUT THIS IMAGE IS NOT" />
</span>
</div>
$('div.myClass span img');
<div class="myClass">
<span>
<a href="#"><img src="THIS IMAGE IS TARGETED" /></a>
<img src="THIS IMAGE IS ALSO TARGETED" />
</span>
</div>
$('div.myClass span img').not('a > img');
<div class="myClass">
<span>
<a href="#"><img src="THIS IMAGE IS ** NOT ** TARGETED" /></a>
<img src="THIS IMAGE IS TARGETED" />
</span>
</div>
You can't make an optional element selector (other than using *
but that's not a great idea). Instead you could bine both the selectors in to a single jQuery object, like this:
$('div.myClass span a>img, div.myClass span img').css('opacity', 0.5);
Note however that if the second selector is valid for your scenario, then the >
selector is redundant anyway.
You can use a context and make your selectors behind it like:
$('a > img, img', 'div.myClass span').css('opacity', 0.5);
And in your case, you only need the img
because a > img
will select the same image:
$('img', 'div.myClass span').css('opacity', 0.5);
Use only
$('div.myClass span img').css('opacity', 0.5);
It will work as your requirements.
You could also cut the expression, and use find
$('div.myClass span').find('a>img, img').css('opacity', 0.5);
I'm trying to select an element through jQuery, but one of the element in the selection is optional. So I have something like this:
$('div.myClass span a>img')
In my scenario, that a
element is an optional one. It may or may not exist in the DOM.
Of course, I can write out the entire line again with one containing a
but this looks verbose:
$('div.myClass span a>img').css('opacity', 0.5);
$('div.myClass span img').css('opacity', 0.5);
Is it possible to define the a
element to be optional in the jQuery selector path?
I'm trying to select an element through jQuery, but one of the element in the selection is optional. So I have something like this:
$('div.myClass span a>img')
In my scenario, that a
element is an optional one. It may or may not exist in the DOM.
Of course, I can write out the entire line again with one containing a
but this looks verbose:
$('div.myClass span a>img').css('opacity', 0.5);
$('div.myClass span img').css('opacity', 0.5);
Is it possible to define the a
element to be optional in the jQuery selector path?
5 Answers
Reset to default 5You only need the a >
if you want to specifically target only images within the anchor. Otherwise, div.myClass span img
will target any image inside the span.... regardless of the presence of an anchor tag.
$('div.myClass span a > img');
<div class="myClass">
<span>
<a href="#"><img src="THIS IMAGE IS TARGETED" /></a>
<img src="BUT THIS IMAGE IS NOT" />
</span>
</div>
$('div.myClass span img');
<div class="myClass">
<span>
<a href="#"><img src="THIS IMAGE IS TARGETED" /></a>
<img src="THIS IMAGE IS ALSO TARGETED" />
</span>
</div>
$('div.myClass span img').not('a > img');
<div class="myClass">
<span>
<a href="#"><img src="THIS IMAGE IS ** NOT ** TARGETED" /></a>
<img src="THIS IMAGE IS TARGETED" />
</span>
</div>
You can't make an optional element selector (other than using *
but that's not a great idea). Instead you could bine both the selectors in to a single jQuery object, like this:
$('div.myClass span a>img, div.myClass span img').css('opacity', 0.5);
Note however that if the second selector is valid for your scenario, then the >
selector is redundant anyway.
You can use a context and make your selectors behind it like:
$('a > img, img', 'div.myClass span').css('opacity', 0.5);
And in your case, you only need the img
because a > img
will select the same image:
$('img', 'div.myClass span').css('opacity', 0.5);
Use only
$('div.myClass span img').css('opacity', 0.5);
It will work as your requirements.
You could also cut the expression, and use find
$('div.myClass span').find('a>img, img').css('opacity', 0.5);
本文标签: javascriptDefining an element in jQuery selector as optionalStack Overflow
版权声明:本文标题:javascript - Defining an element in jQuery selector as optional? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745546039a2155397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论