javascript 使用 jquery 添加和删除内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10182744/
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
appending and removing content with jquery
提问by Blainer
I dont think I am using the .remove correctly because instead of "info about canada" being removed from the div "info", it is stacked on top of "info about russia" instead.
我认为我没有正确使用 .remove,因为它不是从 div“info”中删除“关于加拿大的信息”,而是堆叠在“关于俄罗斯的信息”之上。
js
js
if (code == 'ca') {
$('#info').append('<p class="i">info about canada</p>');
} else if (code == 'ru') {
$('#info').remove('.i');
$('#info').append('<p class="i">info about russia</p>');
}
html
html
<div id="info">
<h3>Info</h3>
</div>
采纳答案by Blainer
I figured it out...I used .empty instead.
我想通了...我用 .empty 代替。
回答by Prestaul
If you are trying to replace the contents of #info
then don't use remove
, empty
, or append
, just use html
:
如果您尝试替换 的内容,#info
请不要使用remove
, empty
, 或append
,只需使用html
:
$('#info').html('<p class="i">info about russia</p>');
回答by udidu
Their is no need to pass parameters to remove
..
他们不需要将参数传递给remove
..
Just do:
做就是了:
$('#info .i').remove();
and it will remove the .i
from the $('#info')
div
它将.i
从$('#info')
div 中删除