【问题标题】:How to exclude Jquery mouseup for a specific element如何为特定元素排除 Jquery mouseup
【发布时间】:2013-01-04 04:07:41
【问题描述】:

我的页面中有这段代码

<body onmouseup="fun()">
.......
.......
<button id="ele">Exclude</button>

</body>

我怎样才能实现这样的目标

function fun()
{
    if(MOUSE RELEASE IS ON TOP OF BUTTON #ele)
    {
        console.log('Mouse released on top of the button');
    }
    else
    {
        console.log('Mouse released');
    }
}

【问题讨论】:

  • 你为什么不为你需要的元素绑定你需要的事件呢?
  • 有很多元素......还有很多代码重组,因为这是一个旧项目......任何其他选择@Lee Taylor

标签: javascript jquery mouseover


【解决方案1】:

如果您使用 jQuery,我建议您使用 .on() 来绑定事件。您可以使用event.target 元素来查看它是否是按钮。

$(document).ready(function(){
    $('body').on('mouseup', fun);
    function fun(event){
        if($(event.target).is('#ele')){
             console.log('Mouse released on top of the button');
        }else{
            console.log('Mouse released');
        }
    }
});

【讨论】:

  • element 来自哪里
  • 谢谢你会检查并回复你
【解决方案2】:

使用event.stopPropagation,像这样:

html

<button id="ele">Exclude</button>
<br>
<br>
<div>fun</div>

js

document.body.onmouseup = function(){ fun(); };
function fun(){
 console.log("fun");   
}
var el = document.getElementById("ele");
el.onmouseup = function(e){ e.stopPropagation(); };

演示:http://jsfiddle.net/GgKsQ/

这是一个使用 jQuery 的演示 mouseup:http://jsfiddle.net/GgKsQ/1/

【讨论】:

  • 感谢+1ed的帮助
【解决方案3】:

这是一个正统的代码,它可以为您提供所需的内容。 请复制到您的编辑器中并检查。

<!DOCTYPE html>
<html>
<body onmouseup="fun()">
The content of the body element is displayed in your browser.
<br/><br/><br/><br/><br/><br/><br/><br/>
<div id="Exclude" onmousedown="fun2()"> Exclude Me Please </div>
</body>
<script>
var idPressed = 0;
function fun() {
if (idPressed == 0)
console.log('Mouse released');
idPressed = 0;
//alert("Whatever "+this.id);
}

function fun2() {
idPressed = 1;
console.log('Mouse released 2');
//alert("Whatever 2"+this.id);
}

</script>
</html>

【讨论】:

  • 感谢+1ed的帮助
  • 感谢您的慷慨。!
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2015-03-28
  • 2011-03-02
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多