动态元素的jQuery 1.7 on()和off()方法 [英] jQuery 1.7 on() and off() methods for dynamic elements

查看:89
本文介绍了动态元素的jQuery 1.7 on()和off()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery 1.7的.on().off()方法应该替换.live()等.

jQuery 1.7's .on() and .off() methods are supposed to replace .live() and such.

我尝试了一个动态项目:

I tried it with a dynamic item:

 $(".myList").on('click', function(e){
  alert('hello world');
 });

这不适用于加载DOM后添加的元素.

This is not working for me for elements added after DOM is loaded.

上面的代码是否是jQuery 1.7的.on().off()方法的有效示例?

Is code above a valid example for jQuery 1.7's .on() and .off() methods?

推荐答案

请参见 http ://blog.jquery.com/2011/11/03/jquery-1-7-released/用于live()-> on/off()(及其他)示例.

See http://blog.jquery.com/2011/11/03/jquery-1-7-released/ for live() -> on/off() (and other) examples.

这是他们将实时转换为on的示例:

This is their example for converting live to on:

$('a').live('click', fn);
$(document).on('click', 'a', fn);

因此您的示例变为:

$(document).on('click', '.myList', function(e){
  alert('hello world');
});

这篇关于动态元素的jQuery 1.7 on()和off()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆