【发布时间】:2025-12-08 08:15:02
【问题描述】:
我无法使用 JQuery 点击功能在“画布”上制作简单的点击事件。
$(document).ready(function(){
alert("test");
$("#canvas").click(function(){
alert("canvas");
});
$(".middle").click(function(){
alert("middle");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="middle">middle
<div id="canvas">canvas</div>
</div>
“测试”消息出现,但上述功能不起作用。 div "middle" 的其他兄弟姐妹(我没有包括,因为我试图将它们删除以进行测试,但它仍然发生)可以执行事件,但只有这个 div 和 inside 不能执行单个事件。
还尝试了console.log($("#canvas")),它在下面获取了一些数据。
[div#canvas]
这里是完整的html:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="/demo/resources/themes/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link href="/demo/resources/themes/custom/css/custom.css" rel="stylesheet" />
<script src="/demo/resources/themes/bootstrap/js/bootstrap.min.js"></script>
<script src="/Sketchy-demo/resources/themes/custom/js/custom.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>App Page</title>
</head>
<body>
<div class="middle">
<div id="canvas"></div>
</div>
</body>
</html>
custom.css
.middle{
z-index: -1;
position: relative;
display: block;
height: 88%;
width:100%;
top: 0px;
left:0px;
margin: 0px;
background-color: transparent;
}
#canvas{
display: block;
position: relative;
height: 88%;
width: 80%;
margin: auto;
top: 6%;
background-color: white;
border: 5px dashed black;
border-radius: 10px;
}
这真的很令人沮丧,因为这是一个我无法调用的基本函数。
编辑:
我添加了https://jsfiddle.net/5mtt2khu/
如有任何问题,请告诉我。 谢谢!
【问题讨论】:
-
当我运行你的代码时,我可以得到警报画布和中间都很好。
-
是的,这就是重点。很容易看出它必须工作,但它不在我的身上
-
你能创建一个minimal reproducible example吗?
-
嗨@ChrisG,这是我得到的所有信息。谢谢
-
是这样的:
.middle { z-index: -1 }它将<div>移动到body后面。知道我是怎么发现的吗?我右键单击画布并选择“检查元素”,然后我被带到<body>。
标签: javascript jquery html css jquery-selectors