相关疑难解决方法(0)

jQuery live('click')触发右键单击

我注意到live()jQuery中函数的奇怪行为:

<a href="#" id="normal">normal</a>
<a href="#" id="live">live</a>

$('#normal').click(clickHandler);
$('#live').live('click', clickHandler);

function clickHandler() {
    alert("Clicked");
    return false;
}
Run Code Online (Sandbox Code Playgroud)

直到您右键单击"实时"链接并触发处理程序,然后不显示上下文菜单,这很好,很花哨.事件处理程序根本不会在"普通"链接上触发(如预期的那样).

我已经能够通过将处理程序更改为此来解决此问题:

function clickHandler(e) {
    if (e.button != 0) return true;
    // normal handler code here
    return false;
}
Run Code Online (Sandbox Code Playgroud)

但是,必须将其添加到所有事件处理程序中真的很烦人.有没有更好的方法让事件处理程序像常规点击处理程序一样点火?

html javascript jquery event-handling

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

event-handling ×1

html ×1

javascript ×1

jquery ×1