jQuery中的jQuery.bind()和jQuery.live()方法有什么区别?
jQuery.bind()方法
bind()jQuery中的方法为所选元素附加一个或多个事件处理程序。
注意:jQuery中bind()不推荐使用jQuery方法。
示例
您可以尝试运行以下代码来学习如何bind()在jQuery中使用方法:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").bind("click", function(){
alert("你好,世界!");
});
});
</script>
</head>
<body>
<div>Click me! I am an alert!</div>
</body>
</html>jQuery.live()方法
live(type,fn)方法将处理程序绑定到所有当前-和将来-匹配元素的事件(如click)。它还可以绑定自定义事件。
这是此方法使用的所有参数的说明:
type:事件类型。
fn:绑定到每个匹配元素集上的事件的函数
示例
您可以尝试运行以下代码来学习如何使用live()方法。
注意:该live()方法在jQuery1.7中已弃用,在1.9版中已删除。因此,如果要运行该live()方法,请使用1.7以下的jQuery版本,如以下代码所示:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/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>热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿