【问题标题】:Jquery doesn't bind to new appended elements [duplicate]Jquery不绑定到新的附加元素[重复]
【发布时间】:2013-11-19 12:17:02
【问题描述】:

我发现默认情况下 JQuery 不会绑定新插入的元素。 这是一个 sn-p,它显示了我要解释的内容。

http://jsfiddle.net/8hf5r/2/

$('#chg-em').on('click', function(e) {
    $('.local').empty().append('<p id="orange-juice">orange juice<p>');
    e.preventDefault();
});

$("#orange-juice").on('click', function() {
    alert('ola');
});

这可能是解决方案? :( 谢谢。

【问题讨论】:

    标签: javascript jquery append element bind


    【解决方案1】:

    使用委托事件:

    $(".local").on('click', '#orange-juice', function() {
        alert('ola');
    });
    

    在此处了解委托事件:http://api.jquery.com/on/#direct-and-delegated-events

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 2016-06-05
      相关资源
      最近更新 更多