2014-02-26 47 views
0

它會完成方法中的所有操作(例如警告/ div加載),但拼接不起作用。所以我想要做的是當用戶點擊「刪除」按鈕時,該特定的視頻從陣列中刪除。我的陣列不會拼接

代碼:

function updateFavourite(video) { 
    document.getElementById("favourite").onclick = function() { 
     blacklist[video["id"]] = true; 
     myfavourite.push(video); 
     var html = 
     "<input class='removeButton' value='Remove' type='button' />" + 
     "<li class=\"saved\">" + 
      "<img class= \"img-rounded\" src=\"{0}\"/>" + 
      "<p><b title=\"{2}\"><a class=\"extendedLink\" href=\"javascript:watchHistoricVideo(\'{1}\');\"><span></span>{2}</a></b><br>" + 
      "by {3}<br>" + 
      "{4} | {5} views</p>" + 
      "</li>"; 

     $("#myfavourite").prepend(html.format(video["thumbnail"], 
     video["id"], 
     video["title"], 
     video["uploader"], 
     video["length"], 
     video["views"])); 
     $("#myfavourite .removeButton").click(function() { 
      myfavourite.splice(video, 1); 
      setVideoF(video); 
      alert("Removed"); 
      document.getElementById("myfavourite").innerHTML = '<div id="myfavourite"></div>'; 
      $("#loadFavourite").trigger('click'); 
     }); 
     setVideoF(video); 
    } 
} 
+8

你還沒有問過這個問題嗎? http://stackoverflow.com/questions/21894835/how-do-i-splice-my-array – putvande

+0

你怎麼知道「我的陣列不會拼接」? – PeeHaa

回答

0

如果您只想刪除video對象,因爲您需要首先在數組中找到它的索引(如doodeec如說)......

function Remove(arr, obj) { 
    var index = arr.indexOf(obj); 

    if (index != -1) { 
     arr.splice(index, 1); 
    } else { 
     throw new Error("Not in array") 
    } 
}; 

只需撥打Remove(myfavourite, video);應該排序。

+0

我添加了一個警告框,它顯然是拼接,但是當我刷新頁面時,它仍然存在。 – bohmygod