不能够点击事件绑定到新创建的跨度项目(jQuery的) [英] Not able to bind click event to newly created span items (jQuery)

查看:100
本文介绍了不能够点击事件绑定到新创建的跨度项目(jQuery的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能将事件绑定到由jQuery的生成的列表。我看了一些类似的问题,但还没有找到任何解决办法。

这是code生成我的名单:

  VAR列表='< UL>';
对于(VAR I = 0; I< data.length;我++)
  {
  清单+ ='<李ID =+数据[I] .ID +'><跨度类=btnRemoveItem称号=&GT从列表中删除项目;< / SPAN>' +数据[I]。名称+'< /李>';
  }
  清单+ ='< / UL>';
  jQuery的(#spanContainer)HTML(列表)。

这造成我的通缉名单。伟大工程。但我不能够操纵名单低谷jQuery的。一些研究指出我的事件绑定到我的列表中的方向。我怎么做?我看有同样的问题几个职位,但没有发现任何解决我的问题。

我使用jQuery 1.2.6,而不能使用V.1.3(其中有.live功能)。

资源:
docs.jquery.com/Events/bind

编辑:
我已经试过这一点,但它不工作。


  

的jQuery(#7276)绑定(点击,函数(){警报('测试');})


这对HTML输出:

 <跨度ID =ITEMLIST>
  < UL>
    <李ID =listItem_7276><跨度标题=从列表中删除项级=btnRemoveItemID =7276/>主营项目< /李>
    <李ID =listItem_7281><跨度标题=从列表中删除项级=btnRemoveItemID =7281/>测试项目4℃; /李>
  < / UL>
< / SPAN>


解决方案

尝试建立数据结构的jQuery:(未经测试code)

  UL = $(< UL />中);
对于(VAR I = 0; I< data.length;我++){
ul.append(
 $(<李/>中)
 .attr(ID,数据[I] .ID)
 .append($(<跨度/>中)
  .addClass(btnRemoveItem)
  .attr(称号,从列表中删除项目)
  。点击(函数(){
   $(本).parent()remove()方法。
  })
 ).append(数据[I]。名称)
);
}
。jQuery的(#spanContainer)的HTML(微升);

I'm not able to bind events to a list that is generated by jQuery. I've looked at some similar issues, but havent found any solution.

This is the code that generates my list:

var list = '<ul>';
for (var i = 0; i < data.length; i++) 
  {
  list += '<li id="' + data[i].id + '"><span class="btnRemoveItem" title="Remove item from list"></span>' + data[i].name + '</li>';
  }
  list += '</ul>';
  jQuery("#spanContainer").html(list);

This creates my wanted list. Works great. But I'm not able to manipulate the list trough jQuery. Some research points me in the direction of binding events to my list. How do I do that? I've looked at several posts having the same issue, but haven't found anything that solved my problem.

I'm using jQuery 1.2.6 and cannot use v.1.3 (which has the .live function).

Resources: docs.jquery.com/Events/bind

Edit: I've tried this, but it's not working.

jQuery("#7276").bind("click", function() { alert('test'); })

This the the html output:

<span id="itemList">
  <ul>
    <li id="listItem_7276"><span title="Remove item from list" class="btnRemoveItem" id="7276"/>Main item</li>
    <li id="listItem_7281"><span title="Remove item from list" class="btnRemoveItem" id="7281"/>Test item 4</li>
  </ul>
</span>

解决方案

Try building the data structure in jquery: (untested code)

ul = $("<ul/>");
for (var i = 0; i < data.length; i++)  {
ul.append(
 $("<li/>")
 .attr("id", data[i].id)
 .append($("<span/>")
  .addClass("btnRemoveItem")
  .attr("title", "Remove item from list")
  .click(function() {
   $(this).parent().remove();
  })
 ).append(data[i].name)
);
}
jQuery("#spanContainer").html(ul);

这篇关于不能够点击事件绑定到新创建的跨度项目(jQuery的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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