且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jQuery检测文档中任何位置的左键单击?

更新时间:2023-02-19 21:18:41

试试

$(document).click(function(e) { 
    // Check for left button
    if (e.button == 0) {
        alert('clicked'); 
    }
});

然而,对于IE是否在左键单击时返回1或0似乎存在一些混淆,可能需要对此进行测试:)

However, there seems to be some confusion as to whether IE returns 1 or 0 as left click, you may need to test this :)

在此处进一步阅读: jQuery Gotcha

编辑:错过了你问的位置不要使用 e.button 。抱歉!

EDIT: missed the bit where you asked not to use e.button. Sorry!

但是,示例此处无论在FF或IE中查看它都会返回相同的ID,并使用e.button。可能已经更新了jQuery框架?另一个答案是IE的旧版本返回不同的值,这将是一个痛苦。不幸的是我在这里只有IE 7/8进行测试。

However, the example here returns the same ID regardless of looking at it in FF or IE, and uses e.button. could possibly have updated the jQuery framework? The other answer is that older versions of IE return a different value, which would be a pain. Unfortunately I only have IE 7/8 here to test against.