如果点击的元素不包含<a> children

时间:2017-07-12 11:44:41

标签: jquery

At the moment,when clicking a box, this box is added a class with this code:

$("#box").click(function(ev) {  
   $(this).addClass("new");
});

Now I want an addition so that the class is only added if that box does not contain a <a> link on the first level (it's children may contain links, but no children may be links). Something along the lines of this:

$("#box").click(function(ev) {  
   $(this).(":not(:has(a))").addClass("new");
});

I hope you understand my intention. Any good methods for this?

1 个答案:

答案 0 :(得分:3)

如下所示: -

$("#box").click(function(ev) {  
   if(!$(this).children('a').length){
     $(this).addClass("new");
   }
});