绑定事件尚未创建DOM元素(jquery) [英] Binding events to not yet created DOM elements (jquery)

查看:114
本文介绍了绑定事件尚未创建DOM元素(jquery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将事件绑定到脚本加载时不存在的HTML元素?



我的脚本的一部分将这些添加到DOM中:

 < a class =btn-remove-itemhref => link< / a> 

问题是我不能这样做:



($)$($)$($)$($)$($($)$
this.parentNode.removeChild(this);
});
});

..我认为因为DOM页面首先加载时不存在。 >

如何将事件绑定到myClass?

解决方案

jQuery.live()已被弃用。以下是使用 jQuery.on()接受的答案:

  $(document).on('click','#btn-remove-item',function(){
this.parentNode.removeChild(this);
});


How do I bind events to html elements that don't exist at the time of the script loading?

One part of my script adds these to the DOM:

<a class="btn-remove-item" href="">link</a>

The problem is I can't just do:

$(document).ready(function(){

    $(".btn-remove-item").click(function(){
        this.parentNode.removeChild(this);
    });
});

.. I think because the DOM element isn't there when the page first loads.

How should I bind the event to myClass?

解决方案

jQuery.live() has been deprecated. Here is the accepted answer using jQuery.on() instead:

$(document).on('click', '#btn-remove-item', function() {
    this.parentNode.removeChild(this);
});

这篇关于绑定事件尚未创建DOM元素(jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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