2010-12-19 332 views
1

我無法移除使用JS創建的對象的孩子的孩子。將孩子從孩子中移除

基本上一旦我創建一個評論對象,我appendChild(replyBox)它。在回覆框內有一個應該完全刪除回覆框的取消按鈕。

下面是代碼:

function Comment(message){ 
    var self = this; 
    var message = message; 

    var comment = document.createElement("li"); 
    comment.id = "comment"; 
    comment.style = "display: none;"; 
    comment.textContent = message; 

    createButtons(comment); 

    var parent = document.getElementById("wall"); 
    parent.appendChild(comment); 
    return comment; 
} 
function deleteComment(comment){ 
    var parent = document.getElementById("wall"); 
    parent.removeChild(comment); 
} 

function newReply(comment){ 
    var buttons = comment.getElementsByTagName("input"); 
    buttons.item(0).disabled="disabled"; 

    var replyBox = document.createElement("div"); 
    replyBox.id="replyBox"; 

    var replyTxt = document.createElement("input"); 
    replyTxt.type="text"; 
    replyTxt.value="Write a reply"; 
    replyTxt.onfocus = "if(this.value==this.defaultValue) this.value='';" ; 
    replyTxt.onblur="if(this.value=='') this.value=this.defaultValue;"; 
    replyBox.appendChild(replyTxt); 

    createButtons(replyBox); 

    comment.appendChild(replyBox); 
} 
function createButtons(parent){ 
    var button = document.createElement("input"); 
    button.type = "submit"; 
    if(parent.id=="comment"){ 
     var reply = button.cloneNode(); 
     reply.value = "reply"; 
     reply.addEventListener("click", function(){newReply(parent)},false); 
     parent.appendChild(reply); 

     var deleteBtn = button.cloneNode(); 
     deleteBtn.value = "delete"; 
     deleteBtn.addEventListener("click", function(){deleteComment(parent)},false); 
     parent.appendChild(deleteBtn); 
    } 
    else{ 
     var submitBtn = button.cloneNode(); 
     submitBtn.value = "submit"; 
     //reply.addEventListener("click", function(){newReply(parent)},false); 
     parent.appendChild(submitBtn); 

     var cancel = button.cloneNode(); 
     cancel.value = "cancel"; 
     cancel.addEventListener("click", function(){cancel(parent)},false); 
     parent.appendChild(cancel); 
    } 
} 

function cancel(replyBox){ 
    replyBox.parentNode.removeChild(replyBox); 
} 

回答

2
cancel.addEventListener("click", function(){cancel(parent)},false); 

cancel是哪個?您有一個名爲cancel的對象以及一個具有相同名稱的函數。嘗試重命名一個。

+0

大聲笑謝謝你......我在這花了太多時間......我忘了一秒鐘,我有兩個不同的東西同名。 – twidizle 2010-12-19 06:17:27

0

我看到這裏有一個問題:

comment.id = "comment"; 

如果你設置comment元素的所有ID,以comment的DOM可以感到困惑。