我有以下jQuery
$('img[title*=\"Show\"]').click(function() {
//$e.preventDefault();
var position = $('img[title*=\"Show\"]').parent().position();
$('#popover').css('top', position.top + $('img[title*=\"Show\"]').parent().height() + 150);
console.log(position);
$('#popover').fadeToggle('fast');
if ($('img[title*=\"Show\"]').hasClass('active')) {
$(this).removeClass('active');
} else {
$('img[title*=\"Show\"]').addClass('active');
}
});
我有两个标题为“显示选项”的图像。出于某种原因,每当我点击任何这些图像时,它都会被打印出来。当我只有1张图片时,它只打印一次。这是为什么?
答案 0 :(得分:3)
而不是$('img[title*=\"Show\"]')
内部点击功能使用$(this)
如果不起作用,请使用:
$('img[title*=\"Show\"]').click(function(e) {
e.stopImmediatePropagation();
//other code
});
答案 1 :(得分:0)
使用以下代码
$('img[title*="Show"]').click(function (evt) {
$('#popover').hide();
$('img[title*="Show"]').removeClass("active");
$(this).addClass("active");
var p = $(this).parent();
$('#popover').css('top', p.position().top + p.height() + 150).fadeToggle('fast');
});
答案 2 :(得分:0)
您可以使用event.stopPropagation,以便事件不会进一步冒泡。也许你的功能是从两个不同的事件触发而另一个也在冒泡时被触发。