替代jquery live可以工作

我有这个简单的代码。 http://jsfiddle.net/borth/BmEZv/如果你点击链接一次,它可以正常工作。 如果再次单击它,则不起作用。 由于在加载DOM后html被加载到html中,我尝试过.on,.bind,.delegate和.live。 除了被弃用的.live之外,它们都没有用(我使用的是jquery 1.7.2)。

有人可以解释为什么.live是唯一有效的function,以及为什么其他function不起作用(或者如果我对其他function做错了)。


$(document).ready(function(){ $(".OpenPopup").bind('click', function(e){ alert('test .OpenPopup'); // do something return false; }); $(".EditIcon").bind('click', function(){ alert('test .EditIcon'); // do something $("#ABC").html('
click here again
'); }); });
click here

 $(document).ready(function(){ $(document.body).on('click', ".OpenPopup", function(e){ alert('test .OpenPopup'); // do something return false; }); $(document.body).on('click', ".EditIcon", function(){ alert('test .EditIcon'); // do something $("#ABC").html('
click here again
'); }); });

.on()可以使用或不使用委托 ,下面是on()使用委托的示例。

 $("#ABC").on('click', ".OpenPopup", function(e){ 

http://jsfiddle.net/BmEZv/1/

在@Dhofca之后,这真的很有效。 我只是展示了一个我尝试使用’this’关键字的示例。

 $(document.body).on('click', ".query-result table tr", function () { var el = $(this); el.closest('table').find('tr').removeClass('dotted'); el.addClass('dotted'); });