第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

原生js怎么實現(xiàn)jquery .on( events, selector, handler )

原生js怎么實現(xiàn)jquery .on( events, selector, handler )

寶慕林4294392 2019-03-05 17:12:26
jquery中如下可以實現(xiàn)事件委派,動態(tài)添加的tr也會觸發(fā)該事件$( "#dataTable tbody" ).on( "click", "tr", function() {  console.log( $( this ).text() );});在原生js中該怎么實現(xiàn)該功能?
查看完整描述

2 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗 獲得超3個贊

function eventDelegate (e,targetSelector,fn) {

    // 兼容性處

    let event = e || window.event;


    // 獲取到目標(biāo)階段指向的元素

    let target = event.target || event.srcElement;


    // 獲取到代理事件的函數(shù)

    let currentTarget = event.currentTarget;


    // 處理 matches 的兼容性

    if (!Element.prototype.matches) {

      Element.prototype.matches =

        Element.prototype.matchesSelector ||

        Element.prototype.mozMatchesSelector ||

        Element.prototype.msMatchesSelector ||

        Element.prototype.oMatchesSelector ||

        Element.prototype.webkitMatchesSelector ||

        function(s) {

          let matches = (this.document || this.ownerDocument).querySelectorAll(s),

            i = matches.length;

          while (--i >= 0 && matches.item(i) !== this) {}

          return i > -1;

        };

    }


    // 遍歷外層并且匹配

    while (target !== currentTarget) {

      // 判斷是否匹配到我們所需要的元素上

      if (target.matches(targetSelector)) {

        let sTarget = target;

        // 執(zhí)行綁定的函數(shù),注意 this

        fn.call(sTarget, Array.prototype.slice.call(arguments))

      }


      target = target.parentNode;

    }


查看完整回答
反對 回復(fù) 2019-03-28
  • 2 回答
  • 0 關(guān)注
  • 634 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號