jQuery on()方法及各个事件绑定方法介绍
来源:互联网 发布:淘宝提货码怎么查 编辑:程序博客网 时间:2024/06/05 06:21
jQuery on()方法是jQuery官方推荐的一个函数,替换live、bind、delegate、和trigger等方法,使用on方法更加的方便和简单。本文将深入介绍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()$(document).ready(function(){$("#div1").on("click","p",function(){$(this).css("background-color","pink");});$("#div2").delegate("p","click",function(){$(this).css("background-color","pink");});});live()$(document).ready(function(){$("#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"); });});
阅读全文
0 0
- jQuery on()方法及各个事件绑定方法介绍
- 完美的jquery事件绑定方法on()
- jQuery的on方法绑定事件
- jQuery新的事件绑定方法on()
- on方法绑定事件
- jquery绑定事件的方法on中获取当前所在元素及子元素的方式
- Jquery事件绑定方法
- jquery on()方法绑定多个选择器,多个事件
- jquery新的绑定事件机制on方法的使用方法
- Jquery on方法绑定事件后执行多次
- jQuery事件绑定on()、bind()与delegate() 方法详解
- jQuery事件绑定on()、bind()与delegate() 方法详解
- jQuery事件绑定on()、bind()与delegate() 方法详解
- jQuery使用on()方法给动态生成元素绑定事件
- jQuery事件绑定on()、bind()与delegate() 方法详解
- jQuery on()方法绑定动态元素的点击事件
- Jquery on方法绑定事件后执行多次
- jQuery中事件绑定bind、live、delegate、on方法
- 操作系统刷题(十五)
- Java CAS和ABA问题
- 单例模式
- git 常用的一些笔记
- 2017/06/07 长威笔试总结
- jQuery on()方法及各个事件绑定方法介绍
- 放一些收集的前端知识点上来,方便查阅
- 最大化平均值(二分搜索)
- PHP如何知道一个类中所有的方法
- C++的一些高级语法
- Nginx虚拟主机配置
- Activiti——数据表结构
- MarkdownPad 2在Window10系统下解决无法使用的问题
- synchronized三种使用方式