i'm trying select element through jquery, 1 of element in selection optional. have this:
$('div.myclass span a>img') in scenario, a element optional one. may or may not exist in dom.
of course, can write out entire line again 1 containing a looks verbose:
$('div.myclass span a>img').css('opacity', 0.5); $('div.myclass span img').css('opacity', 0.5); is possible define a element optional in jquery selector path?
you need a > if want specifically target only images within anchor. otherwise, div.myclass span img target image inside span.... regardless of presence of anchor tag.
$('div.myclass span > img');
<div class="myclass"> <span> <a href="#"><img src="this image targeted" /></a> <img src="but image not" /> </span> </div> $('div.myclass span img');
<div class="myclass"> <span> <a href="#"><img src="this image targeted" /></a> <img src="this image targeted" /> </span> </div> $('div.myclass span img').not('a > img');
<div class="myclass"> <span> <a href="#"><img src="this image ** not ** targeted" /></a> <img src="this image targeted" /> </span> </div>
Comments
Post a Comment