jQuery on()方法

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

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

由此扩展开来的几个以前常见的方法有.

 
 
  1. bind()
  2.   $("p").bind("click",function(){
  3.     alert("The paragraph was clicked.");
  4.   });
  5.   $("p").on("click",function(){
  6.     alert("The paragraph was clicked.");
  7.   });
  8. delegate()
  9.   $("#div1").on("click","p",function(){
  10.     $(this).css("background-color","pink");
  11.   });
  12.   $("#div2").delegate("p","click",function(){
  13.     $(this).css("background-color","pink");
  14.   });
  15. live()
  16.   $("#div1").on("click",function(){
  17.     $(this).css("background-color","pink");
  18.   });
  19.   $("#div2").live("click",function(){
  20.     $(this).css("background-color","pink");
  21.   });

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

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

 
 
  1. $(document).ready(function(){
  2.   $("p").on("click",function(){
  3.     $(this).css("background-color","pink");
  4.   });
  5.   $("button").click(function(){
  6.     $("p").off("click");
  7.   });
  8. });

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

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

trigger()绑定

 
 
  1. $(selector).trigger(event,eventObj,param1,param2,...)
 
 
  1. $(document).ready(function(){
  2.   $("input").select(function(){
  3.     $("input").after(" Text marked!");
  4.   });
  5.   $("button").click(function(){
  6.     $("input").trigger("select");
  7.   });
  8. });

多个事件绑定同一个函数

 
 
  1. $(document).ready(function(){
  2. $("p").on("mouseover mouseout",function(){
  3. $("p").toggleClass("intro");
  4. });
  5. });

多个事件绑定不同函数

 
 
  1. $(document).ready(function(){
  2. $("p").on({
  3. mouseover:function(){$("body").css("background-color","lightgray");},
  4. mouseout:function(){$("body").css("background-color","lightblue");},
  5. click:function(){$("body").css("background-color","yellow");}
  6. });
  7. });

绑定自定义事件

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

传递数据到函数

 
 
  1. function handlerName(event)
  2. {
  3. alert(event.data.msg);
  4. }
  5. $(document).ready(function(){
  6. $("p").on("click", {msg: "You just clicked me!"}, handlerName)
  7. });

适用于未创建的元素

 
 
  1. $(document).ready(function(){
  2.   $("div").on("click","p",function(){
  3.     $(this).slideToggle();
  4.   });
  5.   $("button").click(function(){
  6.     $("<p>This is a new paragraph.</p>").insertAfter("button");
  7.   });
  8. });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值