jQuery on(),live(),trigger()

简介: jQuery on()方法是官方推荐的绑定事件的一个方法。 $(selector).on(event,childSelector,data,function,map); 由此扩展开来的几个以前常见的方法有: bind()   $("p").

 

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

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

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

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()方法。如果你需要移除on()所绑定的方法,可以使用off()方法处理。

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

如果你的事件只需要一次的操作,可以使用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");
  });
});

 

作者:Tyler Ning
出处:http://www.cnblogs.com/tylerdonet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址williamningdong@gmail.com  联系我,非常感谢。

目录
相关文章
|
JavaScript
关于jquery trigger函数执行两次的问题解决
关于jquery trigger函数执行两次的解决方法
181 0
|
JavaScript
jquery(live)中File input的change方法只起一次作用的解决办法
jquery中File input的change方法只起一次作用的解决办法,需要的朋友可以参考下。 错误写法 复制代码代码如下: $(“#uploadImg”).
1251 0
|
JavaScript
jQuery-01:on live bind delegate
摘自:https://www.cnblogs.com/moonreplace/archive/2012/10/09/2717136.html moonreplace这位大牛的       当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的。
1217 0
|
JavaScript
jQuery|事件处理有一个on就够了
on() 方法在被选元素及子元素上添加一个或多个事件处理程序。 自 jQuery 版本 1.7 起,on() 方法是 bind()、live() 和 delegate() 方法的新的替代品。
893 0
|
JavaScript 前端开发
jQuery 事件 - trigger() 方法
$(document).ready(function(){ $("input").select(function(){ $("input").after("文本被选中!"); }); $("button").
813 0
|
JavaScript 前端开发
关于jQuery中的trigger和triggerHandler方法的使用
&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;设计器&lt;/title&gt; &lt;script name="systemJs" type="text/javascript" src="../../zhuant-design/js
1141 0
|
JavaScript 前端开发
Jquery绑定事件(bind和live的区别)
Jquery中绑定事件有三种方法:以click事件为例    (1)target.click(function(){});    (2)target.bind("click",function(){});    (3)target.live("click",function(){}); 第一种方法很好理解,其实就和普通JS的用法差不多,只是少了一个on而已
1222 0