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

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

jQuery: add content to end of div

jquery

提问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 divwithin 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 Andrew Whitaker

Try:

尝试:

$("#region_North_America").append(row_str);

using append().

使用append().

回答by Aidiakapi

Or:

或者:

$("<div>content here</div>").appendTo("#region_North_America");

To create the element on the fly, and place it in the document.
Using the appendTomethod.

动态创建元素,并将其放置在文档中。
使用appendTo方法。

回答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);