When js is used to specify a style or bind an event to an Html element, it is easy to bind the style or event to the parent element of the target element because the browser event is captured/bubbling, if you only want to bind a style or event to a specified target element,
This article helps you understand some of the event bubble knowledge.
I. What is event bubbling?Trigger a certain type of event on an object (such as clicking an onclick event). If this object defines the event handler, this
But we clicked on the three elements that were under the user's mouse pointer, and element is irrelevant to this interaction. Event capture is first handed to the outermost element, and then to a layer of The other opposite strategy is that the most specific elements get a chance to respond before bubbling to more general elements. The solution: Example: 1. Event
Bubble event is to click on the child node, will trigger the parent node up, ancestor node Click event.
We are in peacetime development process, will certainly encounter in a div (this div can be elements) parcel a div situation, but then, in these two div have added events, if click on the div inside we want to deal with this Div event, but then, We don't want
First explain the event bubbling God what,In JS, if you nest a div in a divSuch asstyletype= "Text/css">#box1{width:500px;Height:500px;background:#900;}#box2{width:400px;Height:400px;background:#090;}#box3{width:300px;Height:300px;background:#009;}#box4{width:200px;Height:200px;background:#990;}#box5{width:100px;Height:100px;background:#099;}style>Body>DivID= "Box1"> DivID= "Box2"> DivID= "Box3">
Prevent event bubblingBy default, Knockout allows the Click event to continue bubbling execution on a higher level event handle. For example, if both your element and the parent element are bound to the Click event, then two events will trigger when you click on the element.
The MouseOver () method triggers the MouseOver event, or the function that runs when the MouseOver event occurs
The Mouseout () method triggers the Mouseout event, or a function that runs when the Mouseout event occurs.
First look at an example
code is as follows
copy code
function
collapse operation. However, clicking a label also doesn't happen because it is also a descendant element. In fact, we can not put the check code here, but by modifying the behavior of the button to achieve the goal. 2. Stop Event Propagation. Stoppropagation () method, which can completely prevent event bubbling. Similar to . Target javascript jquery below, w
Examples on the Web are $ (". B"). Live ("Click", Function (event) {}, jquery automatically handles live, preventing event bubbling. If you change it to bind, the experiment will pass.
Copy Code code as follows:
Wrapwrapwrapwrapwrapwrapwrap
Add or not in the above code
Copy Code code as follows:
Event.stoppropagation ();
I. Introduction to bubbling EventsWhen we click on a control, if the parent control that includes the control also has the Click event, it will continue to execute.Method One: Event.stoppropagation ();For example:Paragraph text content Type="Button" Value=Click /> HTML code:Bind the Click event for all DIV elements$("Div").Click( function(
Block bubbling:Bubble Simple example, the son knew a secret message, it told Dad, Dad knew and told Grandpa, a level of transmission so as to cause the chaos of events, and stop bubbling is not let son tell Dad, Dad naturally won't tell Grandpa. The following demo is a good example. x4812 HTML >34 Head >5 Meta CharSet="UTF-8">6 title > title >7 style type="text/css">8#box9width 300px10Height 300px11background Red12Display None13 14 style >15 Script
"Utf-8"> class="Divone"onclick="Divone (This)"> class="Divchild"onclick="Divchild (This)">JS Click child element does not trigger the parent element Click event (JS block bubbling)
This example describes the definition and usage of JavaScript event bubbling. Share to everyone for your reference, specific as follows:
More readers interested in JavaScript-related content can view the site topics: "JavaScript switching effects and tips summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and tips summary", " JavaScript error and debugging skills
Here is just a simple code demo, because the time problem does not do in-depth research, because today when doing the project to block the event bubbling content, find a lot to find a can use, special records.
The above is the entire contents of this article, I hope you can enjoy.
The bubbling event is the order in which the bubbles are triggered from the bottom up to the outside.Window.onload =function() { //The bubbling event is the order in which the bubbles are triggered from the bottom up to the outside.Document.onclick =function() {alert (' Document '); }; Document.documentElement.on
The effect I want to achieve is to click the button to show and hide the Div, click on document to hide the div. However, if there is no cancel event bubbling, the click button will never show Div, because the event layer to bubble up, and finally go to document, directly executed the Click event on the document. In fa
JQuery provides two ways to prevent event bubbling.Mode one: Event.stoppropagation ();$ ("#div1"). MouseDown (function (event) {Event.stoppropagation ();});Mode two: return false;$ ("#div1"). MouseDown (function (event) {return false;});But there is a difference between the two ways. Return false not only prevents the event
The bubbling event is a click on the child node, which triggers the parent node, the ancestor node's click event. In our usual development process, we will certainly encounter a div (this div can be an element) wrapping a div, but, in these two Div added events, if you click inside the div we want to deal with this Div event
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.