jQuery undelegate()method 是一個內置方法,用於從所選元素中刪除指定的事件處理程序。
用法:
$(selector).undelegate(selector, event, function)
參數:該方法接受如上所述和如下所述的三個參數:
- selector:它是一個可選參數,用於指定要從中刪除事件的選擇器。
- event:它是一個可選參數,用於指定選擇器上事件類型的名稱。
- function:它是一個可選參數,用於指定要刪除的處理函數的名稱。
以下示例說明了 jQuery 中的 undelegate() 方法:
示例 1:該示例不包含任何參數。
HTML
<!DOCTYPE html>
<html>
<head>
<title>The undelegate Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</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>
輸出:
注意:首先點擊按鈕,然後點擊段落,然後沒有發生任何變化。
示例 2:該示例包含所有參數。
HTML
<!DOCTYPE html>
<html>
<head>
<title>The undelegate Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</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>
輸出:
注意:如果單擊按鈕,然後單擊 div 元素,則大小不會發生變化。
相關用法
- JQuery undelegate()用法及代碼示例
- JQuery unbind()用法及代碼示例
- JQuery unload()用法及代碼示例
- JQuery unwrap()用法及代碼示例
- JQuery uniqueSort()用法及代碼示例
- JQuery unique()用法及代碼示例
- JQuery andSelf()用法及代碼示例
- JQuery change()用法及代碼示例
- JQuery each()用法及代碼示例
- JQuery end()用法及代碼示例
- JQuery fadeOut()用法及代碼示例
- JQuery height()用法及代碼示例
- JQuery innerHeight()用法及代碼示例
- JQuery keydown()用法及代碼示例
- JQuery keypress()用法及代碼示例
- JQuery next()用法及代碼示例
- JQuery nextAll()用法及代碼示例
- JQuery parent()用法及代碼示例
- JQuery parents()用法及代碼示例
- JQuery prev()用法及代碼示例
- JQuery prevAll()用法及代碼示例
- JQuery remove()用法及代碼示例
- JQuery show()用法及代碼示例
- JQuery toArray()用法及代碼示例
- JQuery width()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 jQuery undelegate() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。