如何替换“实时"从jQuery 1.8.3到jQuery 1.9? [英] How to replace "live" from jQuery 1.8.3 to jQuery 1.9?
问题描述
我的Web框架自动将jQuery脚本更新为当前的最新版本1.9.
My web framework automatically updated my jQuery script to the current last version, the 1.9.
现在我所有的:
$(".myclass").live("click", function() {...
不再工作.我主要将它与一些ajax一起使用,该ajax在页面中填充了html.
don't work anymore. I mostly used it with some ajax called which filled html in my page.
我会知道如何在最新版本中替换此功能.一个朋友告诉我改用"on",但是"on"保持固定在同一元素上.
I would know how to replace this functionnality in the last version. A friend told me to use "on" instead, but the "on" stays fixed on the same element.
说明,在此示例中(无ajax),我使用"+"图标显示"ul li列表".
Explanation, in this example (no ajax), I use a "+" icon, to display an "ul li list".
$(".closed").live('click', function(){
$("#ul_list_"+$(this).attr('id')).addClass("displayed").removeClass("hidden").show();
$(this).addClass("openned").removeClass('closed');
$(this).html('<i class="icon-minus"></i>');
});
$(".openned").live('click', function(){
$("#ul_list_"+$(this).attr('id')).addClass("hidden").removeClass("displayed").hide();
$(this).addClass("closed").removeClass('openned');
$(this).html('<i class="icon-plus"></i>');
});
(我知道该脚本不是有史以来最优化的脚本,但是它可以正常工作.我使用类来打开或关闭列表.如果访问者未启用JS,则不会隐藏任何内容,所有折叠后的列表都会被隐藏打开)
(I know that the script is not the most optimized ever, but it worked. I used classes to open or close my lists. And if the visitor doesn't have JS enabled, nothing is hidden, all the folded lists are opened)
注意:
- 我已经尝试过 https://github.com/jquery/jquery-migrate ,但唯一的消息是我遇到的是"JQMIGRATE:不推荐使用jQuery.fn.live()",而不是解决方法.
- I've tried https://github.com/jquery/jquery-migrate, but the only message that I have is "JQMIGRATE: jQuery.fn.live() is deprecated", not how to fix it.
推荐答案
文档已经提供了一个示例:
The docs already provide an example:
根据其后继者重写.live()方法是 直截了当;这些是所有用户均等呼叫的模板 三种事件附加方法:
Rewriting the .live() method in terms of its successors is straightforward; these are templates for equivalent calls for all three event attachment methods:
$(selector).live(events, data, handler); // jQuery 1.3+
$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+
$(document).on(events, selector, data, handler); // jQuery 1.7+
所以:$(document).on("click", ".closed", function() { ... })
.
这篇关于如何替换“实时"从jQuery 1.8.3到jQuery 1.9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!