如何对(通过js加入的html)实现事件操作

来源:互联网 发布:淘宝卡密自动发货软件 编辑:程序博客网 时间:2024/06/01 08:10

1:在页面中的某个元素实现事件操作

$(element).on('click', function(){});

2:但是当element是通过js来添加的,则上述方法不适用
可使用live才实现事件操作,下面是对live的详解

来自于官方:http://jquery.com/upgrade-guide/1.9/#live-removed

link .live() removedThe .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead. To exactly match $("a.foo").live("click", fn), for example, you can write $(document).on("click", "a.foo", fn). For more information, see the .on() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality.

1.9版本以后包括1.9版本不在支持live的操作。

1.9版本前$(".ding").live('click', function(){    console.log("ok");});1.9版本后$(document).on("click", ".ding", function(){    console.log("ok");});

用jquery新加入的元素会失去事件
下面这种方法解决

$("document").delegate(".ding", "click", function(){     console.log("ok");}); 
0 0