i have a few div's like this:
我有几个这样的div:
test test test testi am adding a new div on the fly:
我正在添加一个新的div:
function ajax() {
$.ajax({
url: '/test/',
type: 'GET',
dataType: "json",
processData: false,
success: function(data) {
var firstDiv = $('.test').find("div:first");
var lastId = firstDiv.attr("id");
console.log(lastId);
var out = '';
$(data).each(function(i) {
messageid = data[i].messageid;
message = data[i].message;
if (messageid != lastId){
if(null != message)
{
out += '<div id="'+ messageid +'"</div>';
$('.test').append(out);
}
}
});
}
});
window.setTimeout("ajax()",1000);
}
basically , if essageid != lastId
and there is a message
append a new div with a id to the main test
div.
基本上,如果essageid!= lastId并且有一条消息将一个带有id的新div附加到主测试div。
the firstDiv
suppose to get the id value of the first div, and it does, but as soon as a new div comes in with a new mesage firstDiv
doesnt update to the new id value but holds the old one.
firstDiv假设获得第一个div的id值,并且确实如此,但是一旦新的div进入新的mesage,firstDiv就不会更新为新的id值而是保留旧的id值。
any idea how to always get the last value of the last div id, even if the new div is added on the fly?. Im not sure how to use live()
or delegate()
here.
任何想法如何总是得到最后一个div id的最后一个值,即使新的div被动态添加?我不知道如何在这里使用live()或delegate()。
thanks
1 个解决方案
#1
0
Check for the last div, instead of the first one.
检查最后一个div,而不是第一个div。
var firstDiv = $('.test').find("div:last");
Your other option is to use prepend instead of append.
你的另一个选择是使用prepend而不是append。
#1
0
Check for the last div, instead of the first one.
检查最后一个div,而不是第一个div。
var firstDiv = $('.test').find("div:last");
Your other option is to use prepend instead of append.
你的另一个选择是使用prepend而不是append。