色色一区二区三区,一本大道道久久九九AV综合,国产香蕉97碰碰视频va碰碰看,综合亚洲国产2020

    <legend id="mljv4"><u id="mljv4"><blockquote id="mljv4"></blockquote></u></legend>

    <sub id="mljv4"><ol id="mljv4"><abbr id="mljv4"></abbr></ol></sub>
      <mark id="mljv4"></mark>

      您現(xiàn)在的位置是:影視綜藝

      js 有沒有辦法 判斷一個dom元素是否已經(jīng)綁定了某個事件,js 判斷是否存有事件 addeventlistener

      2020-10-14 06:17影視綜藝

      簡介addEventListener()綁定事件的對象方addEventListener()含有三個參數(shù),件名稱,另一個是事件執(zhí)行的函數(shù),最后一個是事件捕獲,,obj.addEventListener(" js 有沒有辦法 判斷一個dom元素是否已經(jīng)綁定了某個事件...

      addEventListener()綁定事件的對象方

      addEventListener()含有三個參數(shù),件名稱,另一個是事件執(zhí)行的函數(shù),最后一個是事件捕獲,,

      obj.addEventListener("click",function(){},true/false);

      這里的事件名稱跟直接寫的事件名稱不一樣,在這里前面沒有on,

      還有就是按以往的方法定義事件的話后面的會覆蓋掉前面的事件函數(shù),

      但是按這種方式寫的話幾個事件函數(shù)都會執(zhí)行,

      最后是true和false的解釋,,事件在執(zhí)行時都會有倆個流,一個是捕獲事件流,另一個是冒泡事件流,進(jìn)來的事件是捕獲事件,出去的事件是冒泡事件,true的話會捕獲進(jìn)來時的,false的話會捕獲出去時的,,

      -

      下面是更多關(guān)于addeventlistener的問答

      原生實(shí)現(xiàn)判斷是否有事件。如果確實(shí)需參照代碼,另外本代碼只使用于調(diào)用dom2加載或者移除事件功能,對應(yīng)dom0類型的沒有做測試。

      以下代碼修改了原生的Element對象,是否需要這樣做,請自己酌情處理。

      <!DOCTYPE html>

      <html>

      <head lang="en">

          <meta charset="UTF-8">

          <title></title>

          <script type="text/javascript">

              /**

               * 此處代碼必須放到任何javascript代碼之前。另外增加事件只能用addEventListener形式。

               */

              (function() {

                  Element.prototype.eventListenerList = {};

                  Element.prototype._addEventListener = Element.prototype.addEventListener;

                  Element.prototype._removeEventListener = Element.prototype.removeEventListener;

                  Element.prototype.addEventListener = function(a,b,c) {

                      this._addEventListener(a,b,c);

                      if(!this.eventListenerList[a]) this.eventListenerList[a] = [];

                      this.eventListenerList[a].push(b);

                  };

                  Element.prototype.removeEventListener = function(a,b,c){

                      this._removeEventListener(a, b,c);

                      if(this.eventListenerList[a]){

                          var arr = this.eventListenerList[a];

                          for(var i =0;i<arr.length;i++){

                              if(arr[i].toString() === b.toString()){

                                  this.eventListenerList[a].splice(i,1);

                              }

                          }

                      }

                  }

              })();

              //此后為測試代碼。

              window.onload = function(){

                  var dom = document.getElementById("test");

                  //增加三個監(jiān)聽

                  dom.addEventListener("click",function(){

                      console.info("click function");

                  },false);

                  dom.addEventListener("click",function(){

                      console.info("click function2");

                  },false);

                  dom.addEventListener("click",function(){

                      console.info("click function3");

                  },false);

       

                  console.log(dom.eventListenerList["click"].length);

                  //讀出監(jiān)聽的方法

                  var clicks = dom.eventListenerList.click;

                  if(clicks) clicks.forEach(function(f) {

                      console.log("I listen to this function: "+f.toString());

                  });

                  //刪除監(jiān)聽

                  dom.removeEventListener("click",function(){

                      console.info("click function");

                  },false);

                   

                  console.log(dom.eventListenerList["click"].length);

              };

          </script>

      </head>

      <body>

          <button id="test" >測試</button>

      </body>

      </html>
      變量是否為空?是簡單變量,還是復(fù)合型變量?簡單看就只有空字符串,空數(shù)組,空對象或者未定義變量。一般的if(a){},空數(shù)組就取length屬性,空對象就遍歷對象。 綁定事 有直接在dom寫el.on+type=function(){} 可以通過賦值為null

      這種方式可以typeof el.on+type =='function'

      還有就是addEventListener/attachEvent,就需要用removeEventListener/detachEvent

      這個就難寫了,我覺得如果事件是你自己綁定的,那就在綁定的時候,為元素添加屬性標(biāo)識 本回答被提問者和網(wǎng)友采納 重新聲明一次就好了,比如

      document. body. on click = functio () {}

      直接空函數(shù)即可

      Tags:addeventlistener,js 判斷是否存有事件 a