0

我在动态添加的 html 元素上调用 JavaScript/jQuery 1.9.1 方法时遇到了一些麻烦。

这是我的 JS 代码:

$(".download").live("click", function() {
    var buttonId = $(this).attr("id");
    alert(buttonId);
});

我的动态 HTML 代码:

  var html = "";
    for(var i = 0; i <daten.length; i++) {
       html += "<input id='" + daten[i].pfad + "' class='download' type='image' width='25px' height='25px' src='/download.png'/>";
    }
  $("#table").append(html);

我没有收到错误消息,但是该方法除了没有调用之外,还有什么不正确的?

4

3 回答 3

1

尝试使用on(),live()在较新版本的 jQuery 中已弃用和删除:

$('#table').on('click', '.download', function() {
    alert(this.id);
});

小提琴

对我有用吗?

于 2013-06-28T12:58:56.930 回答
0

改用 .on

$(document).on("click",".download",function() {
    var buttonId = $(this).attr("id");
    alert(buttonId);
});
于 2013-06-28T12:59:37.267 回答
0

像这样使用:

  $(document).on("click",".download",function() {

   //put your code here

  });
于 2013-06-28T13:17:02.483 回答