前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jQuery on()方法

jQuery on()方法

作者头像
陈灬大灬海
发布2019-03-11 11:57:50
1K0
发布2019-03-11 11:57:50
举报

绑定click事件,使用jquery的$().click(func)?别闹了,属实是low。

虽然之前在ajax回调成功后通过dom添加过元素,遇到过这种情况,事件绑定会失效。今天又遇到了,在此总结下。

jQuery on()方法是官方推荐的绑定事件的一个方法。

$(selector).on(event,childSelector,data,function,map)

$().click()和$(document).on('click','要选择的元素',function(){})的

最大的区别即优点是如果动态创建的元素在该选择器选中范围内是能触发回调函数。

$().on()的知识点补充:   1.从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind()、 delegate()、 live()等事件函数。 $().bind()直接绑定在元素上,和click,blur,mouseon一样的点击事件。 $().live()是通过冒泡的方式来绑定到元素上的。更适合列表类型的,绑定到document DOM节点上。 $().delegate()是更精确的小范围的使用事件代理。 $().on()结合了这三个方法的优势摒弃了劣势。

  2.该函数可以为同一元素、同一事件类型绑定多个事件处理函数。触发事件时,jQuery会按照绑定的先后顺序依次执行绑定的事件处理函数。   3. 阻止事件冒泡和事件委托的方法:     A:return false。  在事件的处理中,可以阻止默认事件和冒泡事件。

    B:event.preventDefault()       在事件的处理中,可以阻止默认事件但是允许冒泡事件的发生。

    C:event.stopPropagation().。       在事件的处理中,可以阻止冒泡但是允许默认事件的发生。

jQuery on()扩展开来的几个以前常见的方法有.

bind()
  $("p").bind("click",function(){
    alert("The paragraph was clicked.");
  });

  $("p").on("click",function(){
    alert("The paragraph was clicked.");
  });
delegate()
  $("#div1").on("click","p",function(){
    $(this).css("background-color","pink");
  });

  $("#div2").delegate("p","click",function(){
    $(this).css("background-color","pink");
  });
live()
  $("#div1").on("click",function(){
    $(this).css("background-color","pink");
  });

  $("#div2").live("click",function(){
    $(this).css("background-color","pink");
  });

以上三种方法在jQuery1.8之后都不推荐使用,官方在1.9时已经取消使用live()方法了,所以建议都使用on()方法。

tip:如果你需要移除on()所绑定的方法,可以使用off()方法处理。

$(document).ready(function(){
  $("p").on("click",function(){
    $(this).css("background-color","pink");
  });
  $("button").click(function(){
    $("p").off("click");
  });
});

tip:如果你的事件只需要一次的操作,可以使用one()这个方法

$(document).ready(function(){
  $("p").one("click",function(){
    $(this).animate({fontSize:"+=6px"});
  });
});

trigger()绑定

$(selector).trigger(event,eventObj,param1,param2,...)
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
  $("button").click(function(){
    $("input").trigger("select");
  });
});

多个事件绑定同一个函数

$(document).ready(function(){
  $("p").on("mouseover mouseout",function(){
    $("p").toggleClass("intro");
  });
});

多个事件绑定不同函数

$(document).ready(function(){
  $("p").on({
    mouseover:function(){$("body").css("background-color","lightgray");},  
    mouseout:function(){$("body").css("background-color","lightblue");}, 
    click:function(){$("body").css("background-color","yellow");}  
  });
});

绑定自定义事件

$(document).ready(function(){
  $("p").on("myOwnEvent", function(event, showName){
    $(this).text(showName + "! What a beautiful name!").show();
  });
  $("button").click(function(){
    $("p").trigger("myOwnEvent",["Anja"]);
  });
});

传递数据到函数

function handlerName(event) 
{
  alert(event.data.msg);
}

$(document).ready(function(){
  $("p").on("click", {msg: "You just clicked me!"}, handlerName)
});

适用于未创建的元素

$(document).ready(function(){
  $("div").on("click","p",function(){
    $(this).slideToggle();
  });
  $("button").click(function(){
    $("<p>This is a new paragraph.</p>").insertAfter("button");
  });
});

项目经理是这么说的,还在用.click,你这样面试是会被刷下去的。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档