2011-06-02 76 views
2

我收到此錯誤DOM異常8:JavaScript錯誤:使用jQuery和陣列

Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

這裏是我的代碼(請建議任何事情,使之更有效率/清潔劑):

基本上這有一個按鈕,多數民衆贊成加入它的ID,以稱爲數組「關鍵字」:

$('.add').live('click', function() { 
    if($(this).text() == "+Add") { 
     console.log("add triggered"); 
     $(this).stop().animate({backgroundColor:'#999d92'}, 300); 
     $(this).html("-Rem").fadeIn('fast'); 
     keywords.push($(this).attr("id")); 
     $("#response").append(keywords); 

    } 
    else { 
     $(this).stop().animate({backgroundColor:'#cc6633'}, 300); 
     $(this).html("+Add").fadeIn('fast'); 
     var index = keywords.indexOf($(this).attr("id")); 
     keywords.splice(index, index+1); 
     $("#response").append(keywords); 
    } 

}); 

我希望發生的時候「+添加」推id屬性被添加到陣列,而當-REM推然後重新從關鍵字中移出該ID。

任何建議真的會有所幫助。當我只是追加$(this).attr(「id」)到響應div它打印正確。我也嘗試過使用「String()」函數(可能它是對資源的引用而不是實際的字符串?),並嘗試使用它。

感謝時間!

回答

4
keywords.splice(index, index+1); 

的第二個參數是splice一些項目中刪除,這樣也許應該只是1

$("#response").append(keywords); 

但是keywords是一個字符串數組? jQuery文檔沒有這樣的接口append

你想要這樣的東西嗎?

$("#response").text(keywords.join(', ')); 
+0

非常感謝你! :)我仍然追加在那裏而不是文字 – 2011-06-02 22:53:12