jQuery的从UL与超链接在李
问题描述:
卸下李我有一个无序列表:jQuery的从UL与超链接在李
<ul id="sortable">
<li id="1" class="ui-state-default">First <a href='#' title='delete' class="itemDelete">x</a></li>
<li id="2" class="ui-state-default">Second <a href='#' title='delete' class="itemDelete">x</a></li>
<li id="3" class="ui-state-default">Third <a href='#' title='delete' class="itemDelete">x</a></li>
</ul>
我想从<ul>
删除<li>
。我已经处理了类itemDelete的click事件,我尝试去除它,但是我认为它不起作用,因为我无法在小孩调用它时删除<li>
?
$('.itemDelete').live("click", function() {
var id = $(this).parent().get(0).id;
$("#" + id).remove();
});
什么是最好的方法呢?
答
假设你正在使用的是最新的jQuery的版本:(尽管parent
作品在这里也)
$('#sortable').on('click', '.itemDelete', function() {
$(this).closest('li').remove();
});
closest
是多了几分活力比parent
它得到li
最接近当前元素,在结构中向上。
答
你不必在你<li>
小号
标识如何简单地
$(this).parent().remove();
答
其实,你有它作为现在的样子,id
将是不确定的,因为没有任何的李的有ID。
为什么不只是做
$(this).parent().remove()
也不要忘了返回false。
+0
我确实有id的forgoto将它们粘贴到原始帖子 – Jon 2009-07-13 14:55:14
答
什么清盘工作对我来说: 前缀您的ID与属性字符串或下划线(正如其他人所指出的)
由于像jQuery Mobile的框架要求,IDS是所有网页上的唯一的(不只是在一个页面上,我以页面名称,下划线和数字ID作为前缀,让我访问数据库中的记录。
而不是绑定到类或ul控件,使用'on'绑定到li母公司列表:
$('#sortable').on('dblclick', 'li' function() {
aval = $(this).attr('id').match(/\d+/g); // only want the numbers...
id = aval[0];
name = $(this).text(); // in case you need these for a post...
li = $(this); // so we can remove it from the list after the 'this' object changes inside the ajax call...
// make an ajax call to the server
var jqxhr = $.post("somepage.php", {name: name, id: id},
function(data) {
li.remove();
$("#sortable").listview("refresh");
},'json').fail(function() { alert("error"); });
return false; // preventDefault doesn't seem to work as well...
});
您的ID无效 - ID不能以数字开头 – Greg 2009-07-13 14:56:34
@Jon您的问题对我非常有帮助 – 2012-05-15 16:29:40