我们每 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'
});