简体   繁体   English

如何发生点击功能不是div而是按钮

[英]How to occur click function not div but button

图片1

If I press the boom button, I want only "boom!"如果我按下吊杆按钮,我只想要“轰”! to occur.发生。
But when I click boom first the alert boom pops up, after clicking ok This alert closes and another alert which must pop up only when test is clicked shows up.但是,当我首先单击boom ,警报boom弹出,单击ok此警报将关闭,并且只有在单击测试时才会弹出另一个警报。
If I can't move the button out of the div, how do I handle it?如果我无法将按钮移出 div,我该如何处理?

 $('#divid1 #boom').click(function() { alert("boom!"); }); $('#divid1').click(function() { alert("no?!"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="divid1" style="background-color: #939651; width: 30%; height: 30%;"> <h1>test</h1> <p><button id="boom">boom!</button></p> </div>

Use event.stopPropagation() to prevent the event from bubbling out to the container.使用event.stopPropagation()防止事件冒泡到容器中。

 $('#divid1 #boom').click(function(e) { e.stopPropagation(); alert("boom!"); }); $('#divid1').click(function() { alert("no?!"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="divid1" style="background-color: #939651; width: 30%; height: 30%;"> <h1>test</h1> <p><button id="boom">boom!</button></p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM