Jquery 列表中的点击事件在 Firefox 和其他浏览器中的工作方式不同

Jquery click event on a list works differently in Firefox and other browsers

我目前正在开发动态购物车。过程是这样的:有一个class相同元素的列表,叫做"cartElement"。当用户单击带有此 class 的一个元素时,脚本将获取其 ID 或父级 ID(如果没有)(这是由于不同的元素类型),然后将其发送到另一个函数。

这是脚本:

$('.cartElement').click(function () {
    var id;
    if (event.target.id == '') 
        id = event.target.parentNode.id;
    else 
        id = event.target.id;
    cartRequest(id);
});

这是一个 html 元素:

<div class="sub-addToCart float-lt">
    <button class="addToCart cartElement" id="webreseaux">
       <p class="float-lt">Je veux !</p>
       <i class="icon-cart float-rt"></i>
    </button>
</div>

除了 Firefox 什么都不输出,甚至不输出错误之外,这在所有浏览器中都能完美运行。我试图在不同的点添加 console.log 以查看它在哪里中断。我在 if() 语句之前得到一个 console.log() 输出,但在它之后没有。有什么想法吗?

Firefox 不使用全局事件模型,您必须明确地将其传递给事件回调处理程序:

$('.cartElement').click(function(event){ //<<< pass 'event' here
    /* .... */
});