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

本文涉及的产品
视频直播,500GB 1个月
播放器SDK+直播LIVE组合试用套餐,播放器+流量+转码 1个月
播放器SDK+超低延时直播RTS组合试用套餐,播放器+流量+转码 1个月
简介: 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  联系我,非常感谢。

目录
打赏
0
0
0
0
2
分享
相关文章
关于jquery trigger函数执行两次的问题解决
关于jquery trigger函数执行两次的解决方法
271 0
jquery(live)中File input的change方法只起一次作用的解决办法
jquery中File input的change方法只起一次作用的解决办法,需要的朋友可以参考下。 错误写法 复制代码代码如下: $(“#uploadImg”).
1319 0
jQuery-01:on live bind delegate
摘自:https://www.cnblogs.com/moonreplace/archive/2012/10/09/2717136.html moonreplace这位大牛的       当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的。
1262 0
jQuery|事件处理有一个on就够了
on() 方法在被选元素及子元素上添加一个或多个事件处理程序。 自 jQuery 版本 1.7 起,on() 方法是 bind()、live() 和 delegate() 方法的新的替代品。
941 0
jquery中的trigger和triggerHandler区别
我们在jQuery基础教程之如何注册以及触发自定义事件这篇文章中,有用到今天要讲的trigger方法。今天我们来简单看看jquery中的trigger何triggerHandler方法的区别:trigger( event, [data] )在每一个匹配的元素上触发某类事件。
920 0
|
2月前
jQuery+Slick插件实现游戏人物轮播展示切换源码
jQuery+Slick插件实现游戏人物轮播展示切换源码
45 14
jQuery和CSS3滑动展开菜单按钮插件
这是一款jQuery和CSS3滑动展开菜单按钮插件。该滑动展开菜单按钮在用户点击主菜单按钮之后,子菜单以滑动的方式依次展开
76 21

相关课程

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等