jQuery Cant删除li一旦添加 [英] jQuery Cant remove li once added

查看:102
本文介绍了jQuery Cant删除li一旦添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在列表中添加新的<li>,然后尝试将其删除时,它不会让我从列表中删除新添加的功能吗?

When I add in a new <li> into the list, and then I try to remove it, it won't let me remove newly added features from the list?

http://jsfiddle.net/43nWM/

欢呼

推荐答案

由于浏览器在读取您的JavaScript代码时,动态添加的li不在DOM中,因此无法找到它.而是将处理程序添加到父级,然后像这样监听a:

Cause when the browser is reading your javascript code the dynamically added li isn't in the DOM, so it can't find it. Instead add the handler to the parent, and listen for the a like this:

$('#cblist').on('click', 'a', function(){
    $(this).parent('li').remove();
    return false;
});

提琴: http://jsfiddle.net/43nWM/3/

这篇关于jQuery Cant删除li一旦添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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