How to block the browser's default event, you have encountered the situation can not stop Google default event (native JS) compatibility on the browser scrolling and event binding

If that did not talk much, we look at how to solve

According to different methods of binding events, we have different ways to prevent the default event

If you do not know how to bind to an event, please see my article on  compatibility issues and about the browser scrolling event binding

1. Handle Bind

  Adding one line to the last line of code block

  return false;

2. "addEventListener" Bind

which is:

DOM.addEventListener("click", function (e) {

  var event = e || window.event;

}, false);

  Two cases:

  1. In addition to Chrome browser

   In event.preventDefault added function function ();

  2.Chrome browser

   We need to be false to {passive: false}

Before we talk down to us to explain the function of binding events inside the e What does it mean, in fact, this e is a parameter for FF browser

By this parameter we can get all kinds of parameters "click" events, such as we are still left click on the right mouse button in the current screen (documents, in order to facilitate understanding of the screen we use here, in fact, the screen and the document is a difference coordinates) of

var event = e || window.event; that is, played a role in each browser compatible, more detailed aspects you can go see the other documents

For example, this article:  Why JS event which has a parameter function (ev)?

3. Event "attachEvent" bound

Only a return false;

Here is more interesting, "attachEvent" binding events and the way "addEventListener" bind events like so actually can not use event.preventDefault (); This method

But I think not difficult to understand, after all, "attachEvent" binding event is the browser IE8 or less achieved, but event.preventDefault () method is back out

There is one more interesting thing (the one told you talk about):

attachEvent Although not binding events in IE9 and above browser, but! but! It can be used in more than IE9 browser to prevent the default event (pro-test available)

 

This prevents the default browser on events come to an end, if you have anything more interesting insights welcome message exchange under review

 

 

Original article

 

Guess you like

Origin www.cnblogs.com/hros/p/10926734.html