Javascript 如何将“实时”从 jQuery 1.8.3 替换为 jQuery 1.9?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14479608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:55:53  来源:igfitidea点击:

How to replace "live" from jQuery 1.8.3 to jQuery 1.9?

javascriptjqueryjquery-1.8jquery-1.9

提问by Polopollo

My web framework automatically updated my jQuery script to the current last version, the 1.9.

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

Now all my:

现在我所有的:

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

don't work anymore. I mostly used it with some ajax called which filled html in my page.

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

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.

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

Explanation, in this example (no ajax), I use a "+" icon, to display an "ul li list".

说明,在这个例子中(没有ajax),我使用了一个“+”图标,来显示一个“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>');
});

(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)

(我知道该脚本不是有史以来最优化的,但它有效。我使用类来打开或关闭我的列表。如果访问者没有启用 JS,则不会隐藏任何内容,所有折叠列表都会打开)

Notes:

笔记:

回答by Jon

The docsalready provide an example:

文档已经提供了一个例子:

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+

就其继承者而言重写 .live() 方法很简单;这些是所有三种事件附件方法的等效调用模板:

$(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+

So: $(document).on("click", ".closed", function() { ... }).

所以:$(document).on("click", ".closed", function() { ... })

回答by Rory McCrossan

You need to use onwith a delegated handler:

您需要on与委托处理程序一起使用:

$('#parent').on('click', '.closed', function() {
   // your code...
});

Note that you should replace #parentwith the closest parent element to .closedwhich is available on page load - usually the element which .closedwas appended to.

请注意,您应该替换为页面加载时可用#parent的最近的父元素.closed- 通常.closed是附加到的元素。

回答by muthu

you have to use the oninstead of live. because live is deprecated on the version 1.7

你必须使用on代替live。因为 live 在 1.7 版本中已被弃用