【发布时间】:2016-05-27 02:06:28
【问题描述】:
我试图找出 W3 Schools 的这个例子,但据我所知,它不起作用。如果我遗漏了什么,有人可以指导我吗?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#div1").on("click", function(){
$(this).css("background-color", "pink");
});
$("#div2").live("click", function(){
$(this).css("background-color", "pink");
});
});
</script>
</head>
<body>
<h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and live().</h4>
<div id="div1" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>on() method</b>.</p>
</div><br>
<div id="div2" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>live() method</b>.</p>
</div>
</body>
</html>
小提琴:https://jsfiddle.net/gratiafide/dwmwm43a/
来源:http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_event_on_live
【问题讨论】:
-
W3schools 通常很简单,但已经过时,有时不正确地使用参考网站,而不是像 MDN 或 jQuery 文档。
标签: javascript jquery html