Javascript 如何排除特定元素的Jquery mouseup
我的页面中有这段代码Javascript 如何排除特定元素的Jquery mouseup,javascript,jquery,mouseover,Javascript,Jquery,Mouseover,我的页面中有这段代码 <body onmouseup="fun()"> ....... ....... <button id="ele">Exclude</button> </body> 如果您使用jQuery,我建议使用.on()绑定事件。您可以使用event.target元素查看它是否是按钮 $(document).ready(function(){ $('body').on('mouseup', fun); functio
<body onmouseup="fun()">
.......
.......
<button id="ele">Exclude</button>
</body>
如果您使用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');
}
}
});
使用
event.stopPropagation
,如下所示:
html
演示:
下面是一个使用jQuery的
mouseup
的演示:这是一个正统的代码,它提供了您所需要的。
请复制到编辑器中并检查
<!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>
主体元素的内容将显示在浏览器中。
请把我排除在外
var-idPressed=0;
函数fun(){
如果(idPressed==0)
log(“鼠标释放”);
idPressed=0;
//警报(“无论什么”+this.id);
}
函数fun2(){
idPressed=1;
log('Mouse released 2');
//警报(“任意2”+此id);
}
为什么不为元素绑定所需的事件你需要吗?有很多元素…还有很多代码重组,因为这是一个老项目。@Lee Taylor还有其他选择吗?element
来自哪里谢谢你会检查并返回给你谢谢你的帮助+1
edThanks谢谢你的帮助+1
edThanks谢谢你的慷慨。!
<button id="ele">Exclude</button>
<br>
<br>
<div>fun</div>
document.body.onmouseup = function(){ fun(); };
function fun(){
console.log("fun");
}
var el = document.getElementById("ele");
el.onmouseup = function(e){ e.stopPropagation(); };
<!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>