jQuery | Unelegate()及其示例

Unelegate()方法是jQuery中的一个内置方法,用于从所选元素中删除指定的事件处理程序。

null

语法:

$(selector).undelegate(selector, event, function)

参数: 该方法接受上述三个参数,如下所述:

  • 选择器: 它是一个可选参数,用于指定将从中删除事件的选择器。
  • 活动: 它是一个可选参数,用于指定选择器上事件类型的名称。
  • 功能: 它是一个可选参数,用于指定要删除的处理程序函数的名称。

返回值: 此方法返回所选元素,其中包含Unelegate()方法所做的指定更改。

以下示例说明了jQuery中的Unelegate()方法:

例1: 此示例不包含任何参数。

<!DOCTYPE html>
< html >
< head >
< title >The undelegate Method</ title >
< script src =
</ script >
<!-- jQuery code to show the working of this method -->
< script >
$(document).ready(function() {
$("body").delegate("p", "click", function() {
$(this).css("font-size", "25px");
});
$("button").click(function() {
$("body").undelegate();
});
});
</ script >
< style >
div {
width: 300px;
height: 100px;
background-color: lightgreen;
padding: 20px;
font-weight: bold;
font-size: 20px;
border: 2px solid green;
}
button {
margin-top: 10px;
}
</ style >
</ head >
< body >
< div >
<!-- click on this p element -->
< p >Welcome to GeeksforGeeks!.</ p >
</ div >
<!-- click on this button to remove the
event handler -->
< button >Remove...!</ button >
</ body >
</ html >


输出: 在单击任意位置之前: 图片[1]-jQuery | Unelegate()及其示例-yiteyi-C++库 点击段落后: 图片[2]-jQuery | Unelegate()及其示例-yiteyi-C++库 注: 首先点击按钮,然后点击段落,然后没有变化。

例2: 此示例包含所有参数。

<!DOCTYPE html>
< html >
< head >
< title >The undelegate Method</ title >
< script src =
</ script >
<!-- jQuery code to show the working of this method -->
< script >
$(document).ready(function() {
$("body").delegate("div", "click", function() {
$(this).animate({
height: "+=100px"
});
$(this).animate({
width: "+=100px"
});
});
$("button").click(function() {
$("body").undelegate("div", "click");
});
});
</ script >
< style >
div {
width: 30px;
height: 30px;
background-color: green;
}
button {
margin-top: 10px;
}
</ style >
</ head >
< body >
< div ></ div >
<!-- click on this button -->
< button >Click here..!</ button >
</ body >
</ html >


输出: 单击任意位置之前: 图片[3]-jQuery | Unelegate()及其示例-yiteyi-C++库 单击div元素后,调整大小。 图片[4]-jQuery | Unelegate()及其示例-yiteyi-C++库 注: 如果单击按钮,然后单击div元素,则大小不会发生变化。

© 版权声明
THE END
喜欢就支持一下吧,技术咨询可以联系QQ407933975
点赞13 分享