javascript - 如何将 "live"从 jQuery 1.8.3 替换为 jQuery 1.9?

标签 javascript jquery jquery-1.8 jquery-1.9

<分区>

我的 web 框架自动将我的 jQuery 脚本更新到当前的最新版本 1.9。

现在我所有的:

$(".myclass").live("click", function() {...

不要再工作了。我主要将它与一些调用的 ajax 一起使用,它在我的页面中填充了 html。

我会知道如何在最新版本中替换此功能。一位 friend 告诉我改用“on”,但“on”固定在同一个元素上。

说明,在此示例中(无 ajax),我使用“+”图标来显示“ul li 列表”。

$(".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,则不会隐藏任何内容,所有折叠列表都是打开)

注意事项:

最佳答案

docs已经提供了一个例子:

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() { ... })

关于javascript - 如何将 "live"从 jQuery 1.8.3 替换为 jQuery 1.9?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479608/

相关文章:

javascript - 当我需要用 and 子句分隔查询时, Mongoose 中有两个 "or"子句

javascript - 为什么在文本更改事件后滚动 "Up"? (奇怪的 Action )

javascript - 如何在 JavaScript 中更改添加和删除事件类

jquery - 监听本地存储更改事件

jquery - 使用 jquery 查找最后一个 child

javascript - 避免在 Chrome 中加载页面时触发调整大小事件

php - 如何使用javascript写一个非英文字符的cookie并让php正确读取

javascript - 如何清除 chrome.storage.local 和 chrome.storage.sync?

c# - ASP.NET 中的自动完成 jquery,来自代码隐藏

jQuery .toggle 事件已弃用,该使用什么?