我在 ajax 调用结束时动态获取 div。其中一些 div 具有相同的 html 内容。我希望编写一段代码,让每个唯一内容仅显示一个 div。
查找内容相同的div,只显示其中一个。到目前为止我已经:
onAjaxComplete: function () {
$("div.autocomplete-suggestions > .autocomplete-suggestion").each(function() {
while ($(this).html()) {
//DO SOMETHING HERE
}
});
autocomplete-suggestion
是我的具有相同内容的 div 的类名。
最佳答案
//include this function at the top
function contentInArray(arr,thing){
for(var i=0;i<arr.length;i++){
if (arr[i].innerHTML==thing.innerHTML)return true;
}
return false;
}
//code code code code code
onAjaxComplete: function () {
divs=$("div.autocomplete-suggestions > .autocomplete-suggestion");
arr=[];
for(var i=0;i<divs.length;i++){
if (!contentInArray(arr,divs[i])) arr.push(divs[i])
}
});
现在您拥有了一组具有独特内容的 html 元素
关于javascript - 如果生成的div具有相同的html内容,则只显示一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26015860/