jQuery的$(这)一个.remove()追加后不工作 [英] jQuery $(this).remove() not working after append

查看:210
本文介绍了jQuery的$(这)一个.remove()追加后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div 类=标签一个predefined超链接。

I have a div with class="tags" with one predefined hyperlink.

<div class="tags">
     <a href="#">myLink</a>
</div>

和我有函数删除该超链接,如果用户点击就可以了。

And I have function to remove that hyperlink if user clicks on it.

$('.tags a').click(function() {
    $(this).remove();
    return false;
});

这适用于predefined超链接。
如果我添加一个链接使用jQuery的帮助(在页面加载后)

And this works with predefined hyperlinks. If I add another links with the help of jQuery (after the page is loaded)

$('.tags').append('<a href="#">newLink</a>');

功能去掉超链接(点击上)将不会被这些所谓的,添加链接。如何解决此问题?

Function to remove hyperlink (on click) won't be called on these, added links. How to solve this?

推荐答案

您必须使用实时功能:

$(".tags a").live("click", function() {
    // ...
});

由于您要添加的初始加载后的链接,标准的click事件将不会被绑定到动态添加的链接。

Because you are adding the links after the initial load, the standard click event won't be binded to the dynamic added links.

这篇关于jQuery的$(这)一个.remove()追加后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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