1

JS を使用して作成されたオブジェクトの子の子を削除するのに問題があります。

基本的に、コメント オブジェクトを作成したら、それに appendChild(replyBox) を追加します。replyBox 内には、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);
}
4

2 に答える 2

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

どれcancelがどれですか?呼び出されたオブジェクトとcancel同じ名前の関数があります。名前を変更してみてください。

于 2010-12-19T06:09:54.717 に答える
0

ここに問題があります:

    comment.id = "comment";

コメント要素のすべての ID を に設定するcommentと、DOM が混乱する可能性があります。

于 2010-12-19T06:11:59.973 に答える