jQuery selector for link without an image inside

2019-06-16 06:53发布

问题:

I bind a click function to a link:

$("body").on("click", "a", function() {
    //do something
}); 

Now I am looking for a selector that will only match if the link does not contain an image tag. So kind of similar to a:not(img) but img being the child element.

How can I do that?

回答1:

Try this:

$("body").on("click", "a:not(a:has(img))", function() {
    //do whatever you want here
}); 


回答2:

Try this

$('a:not(:has(>img))').click(function(){
    alert('click');
})​;


回答3:

You can use not with has to filter anchors not having img

Live Demo

$("body").on("click", "a:not(a:has(img))", function() {
    //do something
    alert("");
});