我们每隔5秒进行一次ajax调用,目的是将其内容附加到现有父元素。此父元素最多包含25个元素,因此大多数元素都可以从父元素的开头删除。一切都按预期工作,除了我们输出contentLength的console.log调用。 contentLength的大小不断增加,但它应该是25.我认为这与我们正在进行ajax调用这一事实有关,而且在ajax调用中此时DOM长度“不正确”。难道我做错了什么?我怎么能绕过这个?
AJS.$.ajax({
url: "ajax.action?",
success: function (data) {
//Cleanup and insert into parent div
var divID = "#" + insertContentIntoDiv;
var wrapped = AJS.$("<div>" + data + "</div>");
AJS.$(wrapped).find('title').remove().end();
wrapped = AJS.$(wrapped).find('meta').remove().end();
AJS.$(divID + " dt:last-child").after(wrapped.html().trim());
//Remove any whitespace pre-pended to parent element
cleanWhiteSpaceFromElement(insertContentIntoDiv);
//Remove child elements starting at beginning
var parentElement = document.getElementById(insertContentIntoDiv);
if(parentElement){
var contentLength = parentElement.childNodes.length;
console.log("________________cont: " + contentLength);
if(contentLength > 0){
var difference = contentLength - maxDisplayedLines;
console.log("___________removing: " + difference);
if(difference > 0){
AJS.$(divID + " dt:lt(" + difference + ")").remove();
}
} else {
console.log("ElementID " + insertContentIntoDiv + " appears to have no children. Was this expected?");
}
} else {
console.log("ElementID " + insertContentIntoDiv + " NOT found. Log parsing will NOT be done");
}
},
dataType: 'html'
});