jQuery:事件附加到按钮上,但在Chrome中触发按钮内的图像元素

jQuery: Event is attached to button, but fires at image element inside button in Chrome

本文关键字:按钮 元素 图像元 图像 Chrome 但在 事件 jQuery      更新时间:2023-09-26

我有这样的按钮:

<button class="delete-file-btn" "><img src="/img/del.png"/></button>

然后我通过jQuery:附加事件

$('#Form').on('click', 'button.delete-file-btn', function(e) {
    // Some code here
});

当我在Crome中按下按钮时,e.target指向img元素,而不是按钮。事件分配有什么问题?在FF,一切都很好。

e.target指的是您单击的最具体的元素,即您所针对的元素。事件被激发,因为您的目标是您附加click的元素的子级。使用this引用您附加事件处理程序的元素。

当我尝试你的例子时,我确实得到了e.target的图像,也在firefox中。

我希望这能帮到你!

不确定这是否相关。。但你的<button/>标签中似乎有一个额外的引号(")。这可能会抛弃Chrome,但不会抛弃FF。

我刚刚测试了这个(JSFiddle),通过查看控制台日志,我确实将button.delete-file-btn视为currentTarget