jQuery 如何使用jquery始终将元素添加为最后一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7036095/
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
how to add an element always as last element using jquery?
提问by user811433
I want a certain div to be added as the last element in a list no matter what. Is there any way in which I can explicitly specify this?
无论如何,我都希望将某个 div 添加为列表中的最后一个元素。有什么方法可以明确指定这一点吗?
回答by rkw
$('#list').append('<div></div>')
will append it to the very end of the #list
$('#list').append('<div></div>')
将它附加到#list的最后
If you want to append it to the very last div, just in-case there are other elements after that, then you can use $('#list div:last').after('<div></div>')
如果您想将它附加到最后一个 div,以防万一之后还有其他元素,那么您可以使用 $('#list div:last').after('<div></div>')
回答by jfriend00
Get the parent of the list and add the new item to that parent as the last child.
获取列表的父项并将新项添加到该父项作为最后一个子项。
Using plain javascript:
使用普通的javascript:
parent.appendChild(newElement);
Mozilla documentation: https://developer.mozilla.org/En/DOM/Node.appendChild
Mozilla 文档:https: //developer.mozilla.org/En/DOM/Node.appendChild
If you want to add other items and still have this item be the last item, then you will have to add the new items before this last item or remove this item, append your new item, then append this one back again at the end.
如果你想添加其他项目并且仍然让这个项目成为最后一个项目,那么你必须在最后一个项目之前添加新项目或删除这个项目,附加你的新项目,然后在最后再次附加这个项目。
Once you already have the last element in the list, if you then want to add a new element right before that last element, you can do it like this:
一旦你已经有了列表中的最后一个元素,如果你想在最后一个元素之前添加一个新元素,你可以这样做:
parent.insertBefore(newElement, parent.lastChild);
回答by Tumharyyaaden
If you want to absolutely make sure of that, place empty container with an ID and replace content inside if you need to up date it, so if you have a big container and then sub-containers within, the last child should be like so
如果您想绝对确保这一点,请放置带有 ID 的空容器并在需要更新时替换其中的内容,因此如果您有一个大容器,然后在其中包含子容器,那么最后一个孩子应该是这样
HTML
HTML
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
<div id="child3"></div>
<div id="LastChild"></div>
</div>
now if you need to update,
现在如果你需要更新,
jQuery
jQuery
var newcontent = '<p id="newcontent">I am the new content</p>';
//Build your content as html or plain text
$('#LastChild').html(newcontent); //Replace old content in last child div with new
Now, if you wish to keep the old content from last child div and insert new below that you can do following
现在,如果您希望保留最后一个子 div 的旧内容并在下面插入新内容,您可以执行以下操作
var newcontent = $('#LastChild').html();
var newcontent += '<p id="newcontent">I am the new content</p>';
$('#LastChild').html(newcontent);
Or, if you want to keep old content in last child div but have new content be displayed on top or before the old, you can just swap the order of var newcontent like so
或者,如果您想将旧内容保留在最后一个子 div 中,但要在顶部或旧内容之前显示新内容,您可以像这样交换 var newcontent 的顺序
var newcontent = '<p id="newcontent">I am the new content</p>';
var newcontent += $('#LastChild').html();
$('#LastChild').html(newcontent);
回答by Elon Gomes Vieira
This is working for me.
这对我有用。
$("#content div").last().append("<p>LAST ELEMENT!</p>");
回答by Neelesh
When you are talking about list, I will assume you are referring to un-ordered lists.
当您谈论列表时,我会假设您指的是无序列表。
You will need to so something of this sort:
你将需要这样的东西:
$('ul').append('<li>Bottom list-item</li>');