jQuery.bind() 和 jQuery.live() 方法的區別是什麼?
jQuery.bind() 方法
jQuery 中的 bind() 方法為選定的元素附加一個或多個事件處理程式。
注意:jQuery 中的 bind() 方法已棄用。
示例
您可以嘗試執行以下程式碼,學習如何在 jQuery 中使用 bind() 方法。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").bind("click", function(){
alert("Hello World!");
});
});
</script>
</head>
<body>
<div>Click me! I am an alert!</div>
</body>
</html>jQuery.live() 方法
live( type, fn ) 方法為所有當前和將來的匹配元素繫結事件處理程式(如 click)。它也可以繫結自定義事件。
以下是此方法使用所有引數的說明
- type:事件型別。
- fn:要繫結到匹配元素集上每個元素的事件的函式。
示例
您可以嘗試執行以下程式碼,學習如何使用 live() 方法。
注意:live() 方法在 jQuery 1.7 中已棄用,並在 1.9 版本中移除。因此,如果您想執行 live() 方法,請使用低於 1.7 的 jQuery 版本,如下面的程式碼所示。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("button").live("click", function() {
$("p").slideToggle();
});
});
</script>
</head>
<body>
<p>This is demo text.</p>
<p>This is another text.</p>
<button>Click to toggle</button>
<br><br>
<div>The live() method deprecated in jQuery 1.7, and removed in version 1.9. So, if you want to run the live() method</div>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP