我寫一個簡單的函數,將更新數據的第N頁的最新值的列表,從服務器推。爲了簡單起見,我使用計時器來模擬服務器推送。的jQuery:動態更新
每次服務器消息推,我想更新顯示在頁面上,與放置在列表頂部新接收的消息列表,並最早被丟棄的名單。
這是我到目前爲止有:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<div id="main_cntr">
<ul id="lst">
<li>Line 1 ....</li>
<li>Line 2 ....</li>
<li>Line 3 ....</li>
<li>Line 4 ....</li>
<li>Line 5 ....</li>
</ul>
<div>
</body>
<script type="text/javascript">
// mimics server side push message received at front end
// update list to contain only the last 5 elements
function update_list(newtext){
// Fetch li elements in $('#lst')
// truncate last one
// insert new text to top of li list (ideally, I want to pass the new text to insert to this function)
alert('Got called')
}
$(document).ready(function() {
window.setInterval(update_list, 10000);
});
</script>
</html>
我有2個問題:
- 我如何正確地實現這一點(我不是一個真正的JQuery的專家)
- 如何我修改功能
update_list()
,這樣我可以傳遞一個新的文本(例如隨機文本),從而使服務器推它更好地模擬數據?
難道你不想抓住List的孩子嗎?例如$('#lst')。children()。last()。remove()。end()。end()。prepend(「