0

I've been using http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js for a while now and everything has been fine, but 1.8.3 got quite old so I've decided to move to the latest jQuery. And suddenly a few things stopped working: Quicksand plugin and part of my own jQuery code (that shows additional data under every slide of a slider).

Can you help me figuring out what's wrong? Or maybe it's not worth moving to jQuery versions above 1.8.3? Check the Jsfiddle.

HTML:

<a href="#" class="show" data-show="first">SHOW</a>
<a href="#" class="show" data-show="second">SHOW</a>

<div id="first">First paragraph.</div>
<div id="second">Second paragraph</div>

JS:

jQuery(".show").live("click", function (e) {
    var slide = '#' + jQuery(this).data('show');
    jQuery(slide).slideToggle();
    e.preventDefault();
});

CSS:

div { display: none; }

Here's a working jsfiddle: http://jsfiddle.net/ABrna/

Try changing jQuery to 1.9.1 or 2.0 and hit Run. Script stops working. Why?

4

4 回答 4

4

.live() 方法自 jQuery 1.7 起已被弃用,并已在 1.9 中删除。

http://jquery.com/upgrade-guide/1.9/#live-removed

于 2013-05-14T12:37:33.460 回答
3

等效于.live()使用委托.on()是:

jQuery(document).on("click",".show", function (e) {
    var slide = '#' + jQuery(this).data('show');
    jQuery(slide).slideToggle();
    e.preventDefault();
});

但是您不应该在文档级别设置委托,而是使用最近的静态容器。

于 2013-05-14T12:38:50.697 回答
3

从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应该使用 .delegate() 而不是 .live()。

文档

于 2013-05-14T12:37:44.290 回答
-1

jQuery 2.0 弃用了 live 函数。您可以在http://blog.jquery.com/2013/04/18/jquery-2-0-released/中找到更多信息

于 2013-05-14T12:37:36.290 回答