Javascript 在Jquery中单击事件忽略if/else
我认为这很简单:我只想让JS检测单击链接的子img是否具有特定的src属性。如果是,做一件事。如果没有,请做其他事情。但是由于某些原因,click事件完全忽略了if/else条件。如果使用Javascript 在Jquery中单击事件忽略if/else,javascript,jquery,Javascript,Jquery,我认为这很简单:我只想让JS检测单击链接的子img是否具有特定的src属性。如果是,做一件事。如果没有,请做其他事情。但是由于某些原因,click事件完全忽略了if/else条件。如果使用.children()选择器,我可以随意添加任何废话,事件仍然会像条件为真一样执行。我不太了解Jquery,所以我不知所措。任何帮助都将不胜感激 $('.addcomp').click(function(){ if($(this).children('img[src="add.p
.children()
选择器,我可以随意添加任何废话,事件仍然会像条件为真一样执行。我不太了解Jquery,所以我不知所措。任何帮助都将不胜感激
$('.addcomp').click(function(){
if($(this).children('img[src="add.png"]')){
$(this).parents("tr").find('.compcost').addClass('cost').end().find('td:last-child,td:nth-child(3)').addClass('addcompbg');
$(this).children("img").attr("src","forbidden.png");
}
else if ($(this).children('img[src="forbidden.png"]'))
{$(this).parents("tr").find('.compcost').removeClass('cost').end().find('td:last-child,td:nth-child(3)').removeClass('addcompbg');
$(this).children("img").attr("src","add.png");
}
});
使用$(this.children('img[src=“add.png”]”)代替.length。无论是否存在匹配项,都将始终返回jQuery对象,因此在if
中,它将始终作为true
进行计算