const btns = document.querySelector('#btns')
btns.addEventListener('click', ()=>{
console.log('Hello')
})
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="btns">
<button id="btn1">btn</button>
<button id="btn2">btn</button>
<button id="btn3">btn</button>
<button id="btn4">btn</button>
</div>
</body>
<script src="javascript.js"></script>
</html>
我在一个 div 中有 4 个按钮。如果单击,每个人都会做不同的事情。问题在于,即使用户在按钮周围和按钮之间单击,按钮的传入代码也会执行。我知道我可以通过创建一个 if 语句并告诉按钮仅当值是定义的值时才做某事来解决 javascript 代码中的问题。我想知道是否有一个简单的 css 解决方案。
<div id="choices">
<button type="button" class="btns" id="btn1">?</button>
<button type="button" class="btns" id="btn2">?</button>
<button type="button" class="btns" id="btn3">?</button>
<button type="button" class="btns" id="btn4">?</button>
</div>
最佳答案
我不太了解java脚本,但我可以帮助你
做一个 if 条件说
if (mouseY>"the point of y in south of the button "
&mouseY<"the point of y in north of the button+its height "&
mouseX>"the point of x in west of the button"&
mouseX<"the point of x in east of the button+its width "&mousePressed){execute}
不要复制代码,因为它用于java 处理,但理解它的想法 这个想法是让你的按钮边框
关于javascript - 如果用户点击它们,如何使 div 中的按钮不会被意外点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57715598/