简体   繁体   English

jQuery Live()不起作用

[英]jQuery Live() doesn't work

Hi from some reason my live() function doesn't work. 您好,由于某些原因,我的live()函数无法正常工作。 i want to add a mew li element with click functionality by clicking on li element inside the ulAllApps. 我想通过单击ulAllApps内部的li元素来添加具有单击功能的喵喵li元素。 a new li element created inside the ulMyApps but without the click functionality. 在ulMyApps内部创建了一个新的li元素,但没有单击功能。

HTML: HTML:

<div class="MyApps" >
  <ul class="ulMyApps">   
       <li class="MYLinkTR">app1</li>
   </ul>     
</div>
<div class="AllApps">
   <ul class="ulAllApps"> 
       <li class="IECLinkTR">app1</li>
       <li class="IECLinkTR">app2</li>
   </ul>
</div>

jQuery code: jQuery代码:

$(document).ready(function () {

$(".IECLinkTR").click(function () { 
    var tmp = $(this).html();
    $.ajax({
        type: "POST",
        url: window.location.href+"/addToMyLinks",
        data: "{'app': '" + tmp  + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
           $(".ulMyApps").append("<li class=\"MYLinkTR\">"+ tmp +"</li>");
        },
        error: function (msg) {
           alert("You have already have that app");                 
        }
    }); 
});

$(".MYLinkTR").live('click', function () {
    var tmp = $(this);
    $.ajax({
        type: "POST",
        url: window.location.href + "/removeFromMyLinks",
        data: "{'app': '" + $(this).html() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            tmp.closest('li').remove();
        },
        error: function (msg) {
        }
    });
});
});

from some reason the new li element created dynamically dont have the CLICK functionality coming from the Live function...... 由于某种原因,动态创建的新li元素没有来自Live函数的CLICK功能……

All I can see is that on your MYLinkTR click function you are trying to remove the tmp.closest('li'). 我所看到的是,在您的MYLinkTR click函数上,您试图删除tmp.closest('li')。 Now looking at the docs I think closest is moving up the DOM looking for the closest next ('li') rather then the one it is on. 现在看一下我认为最接近的文档,它正在DOM上移,以寻找最接近的下一个('li'),而不是其最近的那个。 Are you sure you don't want tmp.remove()? 您确定不想要tmp.remove()吗?

Perhaps seeing if an alert is thrown first on the click to see if it is firing as you don't do anything on error. 也许是先查看点击是否引发警报,以查看是否触发了警报,因为您没有对错误采取任何措施。 Something might be happening here that you are not aware of. 您可能不知道在这里发生了一些事情。 The other options is changing LIVE to delegate and attaching this to the ul and see if this fires 其他选项是将LIVE更改为委托并将其附加到ul,看看是否会触发

 $('ul.MyApps').delegate('li', 'click', function(e){
       alert('does this at least fire');
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM