Javascript 如何知道html元素是否被触发
我可以触发Javascript 如何知道html元素是否被触发,javascript,jquery,html,Javascript,Jquery,Html,我可以触发mouseup和mousedown事件。对于触发,我正在使用: $(document).ready(function(){ $("html").mouseup(function(){ console.log("mouseup"); }); $("html").mousedown(function(){ console.log("mousedown"); }); }); 我的问题是,如果我点击空白,我的代码也会被触发。所
mouseup
和mousedown
事件。对于触发,我正在使用:
$(document).ready(function(){
$("html").mouseup(function(){
console.log("mouseup");
});
$("html").mousedown(function(){
console.log("mousedown");
});
});
我的问题是,如果我点击空白,我的代码也会被触发。所以,我只想在用户点击HTML元素时触发,而不是在白色屏幕上。有没有可能做到这一点?现在就开始写,在整个html分区中添加事件。这意味着所有内容都包装在
中代码>标记将响应该事件。将其修复为要响应事件的特定div的标记的名称
例如,对于按钮,它将成为
$('button').on('mouseup', () => {
console.log('mouse up on button')
})`
我检查了你的问题,意识到你想要一些东西。所以我所做的是,我添加了一个条件,在这个条件下,如果目标是html,您的进一步代码将不会执行(只调用事件)
您可以为它使用*
选择器
$(document).ready(function() {
//$("html").find("*").mouseup(function(e) { or
$("html *").mouseup(function(e) {
console.log("mouseup");
e.stopPropagation();
});
//$("html").find("*").mousedown(function(e) { or
$("html *").mousedown(function(e) {
console.log("mousedown");
e.stopPropagation();
});
});
您希望它何时触发?当鼠标单击任何html元素(空格除外)的顶部而不是html目标特定的dom元素而不是$(“html”)
时,您可以将其他dom元素作为目标element@LelioFaieta还有@Yatendrasinh Joddha,我们有很多dom元素。编写所有dom元素而不是html
而不是button是否是一种好方法?是否有可能给出适用于任何dom元素(如button、lable等)的通用标记?您可以随意命名它。这里的关键是,这个div中包含的所有内容都将响应事件。甚至是其中的空白。所以不要把事件放在整个html页面上。尝试使用html类在组中添加事件。好的,thnq:-)我已经按照您所说的做了尝试,但得到的响应与之前相同谢谢您的解决方案
$(document).ready(function() {
//$("html").find("*").mouseup(function(e) { or
$("html *").mouseup(function(e) {
console.log("mouseup");
e.stopPropagation();
});
//$("html").find("*").mousedown(function(e) { or
$("html *").mousedown(function(e) {
console.log("mousedown");
e.stopPropagation();
});
});