Binding multiple responses to the same object and event is executed

1. Because onclick = "" elements added in response to events, add the event to, the event will be added later stacked away, can only be performed in response to the last event

So to use the event listener addElementLitener () to bind multiple handlers, and because of compatibility issues need to be compatible code.

2. In IE8, addElementLitener () This function is incompatible, use attachEvent (). However, this is not compatible with Google, Firefox, so you need to write code compatible

3.addElementLitener () has three parameters, and attachEvent () only two parameters, in response to events in front of one of the former names do not add on, IE needs because it is contrary to his own standards specified for compatibility with other versions of IE is done a concession

4. When the browser does not support an approach, he returns the string undefined

 


<! DOCTYPE HTML>
<HTML lang = "EN">
<head>
  <Meta charset = "UTF-8">
  <title> bind multiple events </ title>
</ head>
<body>
  <= the INPUT of the type " button "value =" I do not spend point "ID =" BTN ">


<Script type =" text / JavaScript ">
  function myget (ID) {
    return document.getElementById (ID);
  }

  function the addEventListener (Element, type, Fn ) {
    // this function from the following method with the same name defined distinction
    IF (typeof element.addEventListener! = "undefined") {
      // is important to note, a string type is undefined, if removed in IE infinite loop
      element .addEventListener (type, Fn, to false);
    } the else IF (typeof element.attachEvent = "!undefined"){
      element.attachEvent("on"+type,fn);
      // respond to events such as click the name should also be written directly express directly what he meant, but you have to give Microsoft a "on" in front of
    } the else {
      Element [ "on" + of the type] = the Fn;
      // If the browser top two two are not compatible, can only write a
    }
  }


  the addEventListener (myget ( "btn"), "the Click", function () {
    console.log ( "Chen Xiaoshuai");
  });
  the addEventListener (myget ( "btn") , "the Click", function () {
    the console.log ( "true");
  });
  the addEventListener (myget ( "BTN"), "the Click", function () {
    the console.log ( "really handsome ah ");
  });
</ Script>

</ body>
</ HTML>

Guess you like

Origin www.cnblogs.com/sherryweb/p/10984005.html