如何使用 jQuery 将 <img > 附加和删除到 <div>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1521846/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:37:44  来源:igfitidea点击:

How to append and remove an <img > to <div> with jQuery?

jquery

提问by Misier

Original:

原来的:

<div id="test">
...
</div>

After appending:

附加后:

<div id="test">
...
<img src=".." />
</div>

After removing:

删除后:

<div id="test">
...
</div>

回答by cpjolicoeur

// add
$("#test").append("<img src='...' />")

// remove
$("#test img:last-child").remove()

回答by Samantha Branham

$('div#test').append('<img src=".."/>');
$('div#test > img').remove();

Note that you can give the image a class as well, in case you only want to remove the one you added.

请注意,您也可以为图像指定一个类,以防您只想删除添加的类。