问题描述
我试图找出W3学校的这个例子,但据我所知,这是行不通的。如果我错过了某些东西,有人可以让我清楚吗? <!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 (#);
});
$(#div2)。live(click,function(){
$(this).css(背景颜色,粉红色);
});
});
< / script>
< / head>
< body>
< h4 style =color:green;>这个例子演示了如何使用on()和live()实现相同的效果。< / h4>
< div id =div1style =border:1px solid black;>这是一些文字。
< p>点击使用< b> on()方法< / b>设置背景颜色。< / p>
< / div>< br>
< div id =div2style =border:1px solid black;>这是一些文字。
< p>点击使用< b> live()方法< / b>设置背景颜色。< / p>
< / div>
< / body>
< / html>
小提琴:
来源:
这是因为 jQuery.fn.live(..);
已被弃用1.7,并在版本1.9中完全删除。
您正在使用 jQuery 1.12.2
,方法 jQuery.fn.live(...);
不存在在此版本的jQuery中。
要获得 jQuery.fn.live(...);
,您必须将脚本
元素更改为此:
< script type =text / javascriptsrc =https://code.jquery.com/jquery- 1.7.min.js>< /脚本>
如果您想使用最新版本的jQuery,请改用它:
<!DOCTYPE html>
< html>
< head>
< script type =text / javascriptsrc =https://code.jquery.com/jquery-1.12.2.min.js>< / script>
< script>
$(document).ready(function(){
$(#div1)。on(click,function(){
$(this).css(background -color,pink);
});
});
< / script>
< / head>
< body>
< div id =div1style =border:1px solid black;>
Hello World!
< p>点击我,让我变成粉红色!< / p>
< / div>
< / body>
< / html>
I'm trying to figure out this example by W3 Schools, but as far as I can tell, it's not working. Could someone steer me clear if I'm missing something?
<!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>
Fiddle: https://jsfiddle.net/gratiafide/dwmwm43a/
Source: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_event_on_live
It is because jQuery.fn.live(..);
has been deprecated in version 1.7 and completely removed in version 1.9.
You are using jQuery 1.12.2
, the method jQuery.fn.live(...);
does not exist in this version of jQuery.
To get jQuery.fn.live(...);
to work you must change the script
element to this:
<script type="text/javascript" src="https://code.jquery.com/jquery-1.7.min.js"></script>
If you would like to use the newest version of jQuery use this instead:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script>
$(document).ready(function() {
$("#div1").on("click", function() {
$(this).css("background-color", "pink");
});
});
</script>
</head>
<body>
<div id="div1" style="border:1px solid black;">
Hello World!
<p>Click me to make me pink!</p>
</div>
</body>
</html>
这篇关于W3学校示例不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!