像按钮一样使用正文页面

时间:2013-07-19 13:14:38

标签: jquery

我使用下面的代码来使用我的页面就像一个按钮,但问题是当我点击其他按钮时这个功能也有效,你有什么建议? document.body.onmousedown = function()

2 个答案:

答案 0 :(得分:0)

jQuery示例:

// Stop bubbling with event.stopPropagation(); on inner buttons
$("#selector").on('click',function(event){
    event.stopPropagation();
    // Run your code
});

$("body").on('click', function(){
    // Run your code
});

答案 1 :(得分:0)

您可以执行Mooseman建议的内容,但您必须将.stopPropagation()添加到所有页内处理程序中,因此这是另一种解决方案。

使用event.target检测事件的来源,如下所示:

$('body').on('click',function(e){
    if (e.target === document.body) {
        alert('body clicked');
    }
});

或使用nodeName:

if (e.target.nodeName.toUpperCase() === 'BODY') {

或jQuery方式:

if ($(e.target).is('body')) {

Here's a fiddle.

相关问题