jQuery中delegate与on的用法与区别
来源:才华咖 本文已影响1.61W人
来源:才华咖 本文已影响1.61W人
在jQuery1.7中 gate()已被()取代。对于早期版本,它仍然使用事件委托的最有效手段。
在事件绑定和委派,delegate()和on在一般情况下,这两种方法是等效的。
gate() 指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。
复制代码 代码如下:
// jQuery 1.4.3+
$( elements )gate( selector, events, data, handler );
// jQuery 1.7+
$( elements )( events, [selector], data, handler );
例如:gate() code:
复制代码 代码如下:
$("table")gate("td","click",function(){
alert("hello");
});
() code:
复制代码 代码如下:
$("table")("click", "td", function() {
alert("hi");
});
PS: 两者区别是seleter和events顺序不同
delegate和on方法被选元素的子元素必须是"合法的"子元素。比如
复制代码 代码如下:
$("table")gate("button","click",function(){...});
$("table")("click", "p", function(){...});
就不起作用,因为正常情况下,table子元素应为tr,td...
on(events,[selector],[data],fn),参数[selector]是可选,
一个选择器字符串用于过滤器的触发事件的选择器元素的.后代。
例如:
复制代码 代码如下:
$("table")("click", "1", function() {
alert("hi");
});
过滤class为td1的table子元素
而delegate的selector是必需的。
jQuery中nextAll()方法用法欣赏
How Do My Parents Feel About My Education英语作文
Equal Rights between Men and Women高中英语作文
MS SQL Server Management Studio Express安装图文具体步骤
Module 6 Unit 1 I went there last year教学反思
Java中break、continue、return语句的使用区别对比
Jolie drops battle over perfume named Shilohy英语作文
初三年级英语课文复习 Unit 1. Try not to translate every word
高中优秀英语作文An Unforgettable Experience
英语满分作文:Reading-TheBestMeanstoAcquireknowledge
有关jQuery效果 slideToggle() 方法(在隐藏和显示之间切换)
英语阅读:VIT declines to acquire Ang Mo Kio property
英语作文:Reading Outside The College
jquery中toggle方法使用例子
让jQuery Mobile不显示loading界面的方法
法律执行与法律适用的区别
英语阅读:Feelings of joy and thoughts from joy
最新《Fate/Stay Night Unlimited Blade Works》经典名句
A letter to the young高中生作文
英语作文:My Generous Grandfather
Modern Technology Makes Life More Conv英语六级作文
快速入睡的方法The Ways to Get Sleep Quickly高中英语作文
A Lifeguard at Johns Beach Saved a Drowning Child,A Lifeguar英语作文
jQuery中ajax的get()方法运用示例
英语习惯用语:be led by the nose; under your nose
Module 8 Unit 2 We are turning around英语课堂教学反思
bedependentfrom与on区别
An unforgettable holiday英语作文
To Be a Good Talker and Listener初中英语作文
常用文种的用法与区别
《The Prevalence of Western Holidays》英语四级作文
英语四级作文 Learn to Stand on Your Own Feet(学会自立)
jQuery中delegate与on的用法与区别
八年级《unit8 Why don’t you get her a scarf? Period1》评课稿
dream of the journey初中英语作文
关于javascript中encodeURI和decodeURI的使用方法
如何实现bootstrap jquery dataTable异步ajax刷新表格数据
Jquery中extend扩展方法的三个原型
《You Cant Judge a William by Its Cover》英语美文欣赏
java中extends与implements区别