2016-05-29 55 views
0
function sort(type) { 

//EMPTY FUNCTION IS CALLED HERE FOR REMOVING ALL PREVIOUSLY CREATED CHILD DIVS 

$("#parentDiv").empty(); 
$.get("http://www.omdbapi.com/?s=Batman&page=2", ({ Search }) => { 
    Search.sort((a, b) => a[type] > b[type]); 
    console.log(`Sorted by: ${type}`); 
    console.log("movies are displayed!!"); 
    var i; 

Loop从这里开始创建div元素。当第二次调用该函数时,子div再次创建

for(i=0;i<Search.length;i++){ 
    var title=Search[i].Title; 
    var year=Search[i].Year; 

父div包含h2和p存在的子div。

var parentDiv=document.createElement("div"); 
    parentDiv.setAttribute("id","parentDiv"); 
    parentDiv.setAttribute("id","childDiv"); 

孩子的div和h2的和对的决策和追加到孩子的div从这里开始

var childDiv=document.createElement("div"); 
    var movieElement=document.createElement("h2"); 
    movieElement.setAttribute("id","moviename"); 
    var yearElement=document.createElement("p"); 
    yearElement.setAttribute("id","year"); 
    childDiv.appendChild(movieElement); 
    childDiv.appendChild(yearElement); 
    parentDiv.appendChild(childDiv); 
    document.body.appendChild(parentDiv); 

    //document.getElementsByTagName("h2")[i].innerHTML="<b>" + title + "</b><br>"; 
    //document.getElementsByTagName("p")[i].innerHTML= year + "<br>"; } 
}); 

}

当这个函数被调用第二次孩子的div再次创造虽然我首先声明为空(在第二行排序函数中)父div中的元素,然后从开始创建子div。

function show(){ 
var z=document.getElementById("sortOrder").value; 
sort(z); 
//sort(Year) 
//sort(title) 
}; 

回答

0

除非它是一个错字,你重写你的父母div的ID:

var parentDiv=document.createElement("div"); 
parentDiv.setAttribute("id","parentDiv"); 
parentDiv.setAttribute("id","childDiv"); // <-- 
+0

无那没用 –

0

问题是父DIV也总是创造,因为它是在for循环使空移除了

相关问题