可以有效的替代jQuery Live [英] Alternative to jquery live that can work

查看:93
本文介绍了可以有效的替代jQuery Live的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this simple code. http://jsfiddle.net/borth/BmEZv/ If you click on the link once, it works fine. If you click on it a second time, it doesn't work. Due to the html being loaded into html after the DOM has loaded, I've tried .on, .bind, .delegate, and .live. No of them work except for .live which is being deprecated (I'm using jquery 1.7.2).

有人可以解释为什么.live是唯一起作用的功能,为什么其他人不起作用(或者我是否对其他功能做错了事情).

Can someone explain why .live is the only function that works and why the others don't work (or if I am doing something wrong with the other functions).

$(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('<div class="EditIcon OpenPopup" pwidth="800" pheight="500" ptitle="EditText">click here again</div>');
  });
});


<div id="ABC"><div class="EditIcon OpenPopup" pwidth="800" pheight="500" ptitle="EditText">click here</div></div>

推荐答案

$(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('<div class="EditIcon OpenPopup" pwidth="800" pheight="500" ptitle="Edit Text">click here again</div>');
        });
    });

这篇关于可以有效的替代jQuery Live的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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