84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
I have the following code:
<div onclick="myFunc()" style="height:200px;width:200px;"> <button></button> </div>
I want myFunc to be executed when anywhere on the div is clicked, but not the button. How can I achieve this?
In the button click event, you need to cancel event propagation. Or stop "bubbling".
See https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
So in your button's click event, you need to be like this:
function button_click(event) { event.stopPropagation(); console.log("按钮被点击了。"); }
By default, the click event of an element will be passed to its parent element.
In the button click event, you need to cancel event propagation. Or stop "bubbling".
See https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
So in your button's click event, you need to be like this:
By default, the click event of an element will be passed to its parent element.