jq简单实现事件委派

同自定义属性委派的事件,查找速度较快,当然在事件的触发过程中也要注意事件的冒泡,和捕获,这些有时候会造成一些负面影响。

 <button type="button" oper="myBtn"></button>
$("body").on("click",'[oper]',function(e){
        let _this = $(this);
        let type = _this.attr('oper');
        e.stopPropagation();//阻止事件冒泡
        switch(type){
          case "myBtn":
            console.log("委派成功!")
            break;
        }
})

猜你喜欢

转载自blog.csdn.net/weixin_49666910/article/details/113524066