Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does JavaScript prevent events from bubbling and browser default behavior

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces JavaScript how to prevent event bubbling and browser default behavior related knowledge, the content is detailed and easy to understand, easy to operate, has a certain reference value, I believe you will learn something after reading this JavaScript article on how to prevent event bubbling and browser default behavior, let's take a look at it.

1. Prevent event bubbling compatible with W3C browsers

Function cBubble (e) {

If (e.stopPropagation) {

E.stopPropagation (); / / ie9+

} else {/ / ie678

E.cancelBubble = true

}

}

two。 Block browser default behavior

Function cDefault (e) {

If (e.preventDefault) {/ / ie9+

E.preventDefault ()

} else {/ / ie 678

E.returnValue = false

}

}

3. Block default details

The method of W3C is e.preventDefault ()

IE uses e.returnValue = false

PreventDefault it is a method of the event object (Event)

The function is to cancel the default behavior of a target element.

Since we are talking about the default behavior, of course, the element must have the default behavior before it can be canceled.

If the element itself has no default behavior, the call is of course invalid.

What element has default behavior?

Such as links

Submit button, etc.

When the cancelable of the Event object is false, there is no default behavior

Even if there is a default behavior, calling preventDefault will not work.

This is the end of the article on "how to prevent JavaScript from bubbling events and browser default behavior". Thank you for reading! I believe you all have a certain understanding of "how to prevent events from bubbling and browser default behavior". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report