2011-08-12 32 views

回答

10

獲取列表的父項並將新項目添加到該父項作爲最後一個子項。

使用普通的JavaScript:

parent.appendChild(newElement); 

Mozilla的文檔:https://developer.mozilla.org/En/DOM/Node.appendChild

如果要添加其他項目,仍然有這個項目是最後一個項目,那麼你將不得不之前添加新項目最後一個項目或刪除這個項目,追加你的新項目,然後在最後追加這個項目。

一旦你已經擁有的最後一個元素列表中,如果你那麼想的是最後一個元素前右添加新的元素,你可以做這樣的:

parent.insertBefore(newElement, parent.lastChild); 
11

$('#list').append('<div></div>')將其追加到在#list的盡頭

如果你想將其追加到最後的div,只是在情況還有其他因素後,那麼你可以使用$('#list div:last').after('<div></div>')

2

如果你想絕對確保的那就是放置空的conta與ID核能研究所和內更換內容,如果您需要了日期,所以如果你有一個大的容器內,然後子容器,最後一個孩子應該像這樣

HTML

<div id="parent"> 
    <div id="child1"></div> 
    <div id="child2"></div> 
    <div id="child3"></div> 

    <div id="LastChild"></div> 
</div> 

現在,如果你需要更新,

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 

現在,如果你想保留的最後一個子DIV舊的內容,並插入新的下面,你可以做以下

var newcontent = $('#LastChild').html(); 
var newcontent += '<p id="newcontent">I am the new content</p>'; 

$('#LastChild').html(newcontent); 

或者,如果你想保留舊的內容中最後一個子DIV,但有新的內容顯示在頂部或以前的老,你可以換VAR newcontent的順序,像這樣

var newcontent = '<p id="newcontent">I am the new content</p>'; 
var newcontent += $('#LastChild').html(); 

$('#LastChild').html(newcontent); 
-1

當你在談論名單,我會假設你指的是未有序列表。

你需要這樣的東西這類的:

$('ul').append('<li>Bottom list-item</li>'); 
相關問題