简体   繁体   中英

How can check if the anchor text contain image tag using jquery?

How can i check if the anchor text contain image tag using jquery. I tried here is the
Fiddle .

For example:

<a href="link"><img src="image.jpg"/></a>

Is it possible to check the condition if the anchor text contain img src then do something.

Any Suggestion Would be Great.

Thanks, vicky

You can use Jquery has()

   if($("a").has("img")){
  // do some thing here 
  }   

Reference : jquery filtering has + not

You must to compare if .length is greater than 0 — if yes, there is an image.

Follow the example:

if($('a').find('img').length > 0) {
   alert("There is an image.");
} else {
   alert("There isn't an image.");
}

jsFiddle right here.

Try .has() it reduces the set of matched elements to those that have a descendant that matches the selector or DOM element. :

if($("a").has("img")) { 
    alert("Yes it has"); 
} else { 
    alert("No it donot"); 
}

See more about it here: http://api.jquery.com/has/

See the demo (jsFiddle).

Just to add more options if the has() function does not work, then you can use this:

jQuery("a > img").on("click", function(){
   //alert("image anchor!");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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