jQuery:将内容添加到 div 的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5290507/
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
jQuery: add content to end of div
提问by Andrew Whitaker
I have this HTML:
我有这个 HTML:
<div class="region-list" id="region_North_America">
<strong>North America</strong>
</div>
and want to add more divs after the strong element to result:
并希望在强元素之后添加更多 div 以产生结果:
<div class="region-list" id="region_North_America">
<strong>North America</strong>
<div> ... </div>
<div> ... </div>
<div> ... </div>
</div>
I am trying this:
我正在尝试这个:
var row_str = '<div>content here</div>';
$('#region_North_America div:last').html(row_str);
However, there is no change to the html. This is probably so since there is no div
within the element selected.
但是,html 没有变化。这可能是因为div
所选元素中没有。
I know that the js is making it to this code because I can print the content of row_str to the console.
我知道 js 正在使用此代码,因为我可以将 row_str 的内容打印到控制台。
So, how can I get to the end of that container element to add the new items?
那么,如何到达该容器元素的末尾以添加新项目?
Thx.
谢谢。
回答by Aidiakapi
回答by Trevor
Your code will just place html in the last div within #region_North_America. Use the append function.
您的代码只会将 html 放在 #region_North_America 内的最后一个 div 中。使用附加功能。
$("div.region-list").append(row_str);