Javascript jQuery.remove()赢得';不要删除元素
我尝试从HTML页面动态删除Javascript jQuery.remove()赢得';不要删除元素,javascript,jquery,html,removechild,Javascript,Jquery,Html,Removechild,我尝试从HTML页面动态删除li元素,我使用.remove()方法,但是,如果我检查HTML页面,我可以看到元素没有被删除,它只是将他的可见性更改为显示:无。 是否有一种方法可以最终从页面中删除该元素 这是代码片段(我从#ul1克隆了一个li,并将其附加到#ul2): 使用slideUp的“complete”回调函数 () 这不起作用的原因是您正在setTimeout子函数中调用$(this)。您需要将元素定义为变量,以便在子函数中访问它 $(oggettoClone).bind({ c
li
元素,我使用.remove()
方法,但是,如果我检查HTML页面,我可以看到元素没有被删除,它只是将他的可见性更改为显示:无代码>。
是否有一种方法可以最终从页面中删除该元素
这是代码片段(我从#ul1
克隆了一个li
,并将其附加到#ul2
):
使用slideUp的“complete”回调函数
()
这不起作用的原因是您正在setTimeout子函数中调用$(this)
。您需要将元素定义为变量,以便在子函数中访问它
$(oggettoClone).bind({
click: function(o) {
var r = confirm("Remove this element?");
var t = $(this);
if (r == true) {
$(t).slideUp();
setTimeout(function(){
$(t).remove();
}, 2000);
}
}
});
或者遵循andi的建议并使用的回调选项:
$(oggettoClone).bind({
click: function(o) {
var r = confirm("Remove this element?");
if (r == true) {
$(this).slideUp(function(){
$(this).remove();
});
}
}
});
你能在www.jsfiddle.net中复制它吗?.remove()按照它在tin上所说的做,它从DOM树中删除元素,以及其中的所有内容。在视图中检查源代码,对吗?如果是这样的话,你的应用程序中似乎已经重新定义了。remove()
等同于。hide()
。啊,感谢代码更新-设置超时是您的问题;当您在setTimeout范围内使用$(this)时,这往往会导致问题。
$(oggettoClone).bind({
click: function(o) {
var r = confirm("Remove this element?");
var t = $(this);
if (r == true) {
$(t).slideUp();
setTimeout(function(){
$(t).remove();
}, 2000);
}
}
});
$(oggettoClone).bind({
click: function(o) {
var r = confirm("Remove this element?");
if (r == true) {
$(this).slideUp(function(){
$(this).remove();
});
}
}
});