jQuery的单击事件的行为不同,在Firefox中肝功能肝功能、单击、不同、行为

2023-09-10 19:42:25 作者:红裤衩避邪

使用事件点击与肝功能使用Firefox *时,会导致奇怪的行为。 由于住在Firefox中,点击时触发右键单击也!同样的情况不会发生在Internet Explorer 7无论是在谷歌浏览器。

Using the event click with live function leads to strange behavior when using Firefox*. With live in Firefox, click is triggered when right-clicking also! The same does not happen in Internet Explorer 7 neither in Google Chrome.

例如:

没有活,去演示,并尝试右击 该段落。如果一个对话框菜单 出现。 随着现场,去演示和试权 点击点击我!。现在,这两个对话 菜单和另一款的出现。 Without live, go to demo and try right clicking the paragraphs. A dialog menu should appear. With live, go to demo and try right clicking "Click me!". Now both dialog menu and "Another paragraph" appear.

*与Firefox 3.5.3测试

*tested with firefox 3.5.3

推荐答案

据我所知,这是一个已知的问题(错误?)。您可以轻松地解决它通过测试该按钮被点击如下:

As far as I know, that is a known issue (bug?). You can easily work around it by testing which button was clicked as follows:

$('a.foo').live("click", function(e) { 
    if (e.button == 0) { // 0 = left, 1 = middle, 2 = right 
        //left button was clicked
    } else {
        //other button was clicked (do nothing?)
        //return false or e.preventDefault()
    }
});

您可以使用开关根据您的具体要求preFER,但通常你可能只想做什么(或,或简单地返回),如果任一按钮比左边的按钮之外被点击,如上:

you might prefer using a switch depending on your specific requirements, but generally you would probably just want to do nothing (or or simply return) if any button other than the left button is clicked, as above:

    $('a.foo').live("click", function(e) {
        switch(e.button) {
            case 0 : alert('Left button was clicked');break;
            default: return false;
        }
    });