Javascript 与 jquery 中的追加相反
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4934488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opposite of append in jquery
提问by sami
I use .append
to add to a div
我.append
用来添加到 div
$(this).append('<ul><li>test</li></ul>');
how can I search fo a <ul>
and remove it if it exists in the children of $(this)
?
<ul>
如果它存在于 的孩子中,我该如何搜索并删除它$(this)
?
回答by Josh K
You could use remove()
. More information on jQuery remove().
你可以使用remove()
. 有关jQuery remove() 的更多信息。
$(this).children("ul").remove();
Note that this will remove allul
elements that are children.
请注意,这将删除所有子ul
元素。
回答by elbowlobstercowstand
The opposite of .append()
is .prepend()
.
的反义词.append()
是.prepend()
。
From the jQuery documentation for prepend…
The .prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use .append()).
.prepend() 方法将指定的内容作为 jQuery 集合中每个元素的第一个子元素插入(要将其作为最后一个子元素插入,请使用 .append())。
I realize this doesn't answer the OP's specificcase. But it does answer the question heading.:) And it's the first hit on Google for “jquery opposite append”.
我意识到这并不能回答 OP 的具体情况。但它确实回答了问题标题。:) 它是 Google 上第一次因“jquery 反向追加”而受到欢迎。
回答by RoToRa
What you also should consider, is keeping a reference to the created element, then you can easily remove it specificly:
您还应该考虑的是,保留对创建元素的引用,然后您可以轻松地将其删除:
var newUL = $('<ul><li>test</li></ul>');
$(this).append(newUL);
// Later ...
newUL.remove();
回答by Martin Wirth
just had the same problem and ive come across this - which actually does the trick for me:
刚刚遇到了同样的问题,我遇到了这个 - 这实际上对我有用:
// $("#the_div").contents().remove();
// or short:
$("#the_div").empty();
$("#the_div").append("HTML goes in here...");