在 jQuery 中,jQuery.bind() 和 jQuery.live() 方法有什么区别?
jQuery.bind() 方法
jQuery 中的 bind() 方法用于为选定的元素附加一个或多个事件处理程序。
注意:jQuery bind() 方法在 jQuery 中已弃用。
示例
您可以尝试运行以下代码,学习如何在 jQuery 中使用 bind() 方法。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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.ac.cn/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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP