jquery 在最后一个元素之前追加

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

jquery append before final element

jquery

提问by GSto

I have html structured like so.

我有这样的 html 结构。

<div id='container'>
  <div class='item'> ... </div>
  <div class='item'> ... </div>
  <div class='item'> ... </div>
  ...
  <div id='item_final'><input type="button" id='addOne'>...</div>
</div>

and what I am trying to do it add another item to the container class before the item_final div. The items are dynamic, so the number of them is unkown.

以及我正在尝试将另一个项目添加到 item_final div 之前的容器类。这些项目是动态的,因此它们的数量是未知的。

回答by Stefan Kendall

$('#container div:last').before( $('<div>') );

回答by David Hellsing

Try the .insertBeforemethod.

尝试.insertBefore方法。

$('<div>').insertBefore('#item_final');