将孩子从孩子身上移走

发布于 2024-10-08 11:20:22 字数 2223 浏览 7 评论 0原文

我在删除使用 JS 创建的对象的子对象的子对象时遇到问题。

基本上,一旦我创建了一个评论对象,我就会将Child(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);
}

I am having trouble removing the child of a child of an object created using JS.

Basically once I create a comment object I appendChild(replyBox) to it. Inside the replyBox there is a cancel button which is supposed to completely delete the replyBox.

Here is the code :

 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2024-10-15 11:20:22
   cancel.addEventListener("click", function(){cancel(parent)},false);

哪个取消是哪个?您有一个名为 cancel 的对象以及一个同名的函数。尝试重命名一个。

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

Which cancel is which? You have an object called cancel as well as a function with the same name. Try renaming one.

十年九夏 2024-10-15 11:20:22

我在这里看到一个问题:

    comment.id = "comment";

如果您将评论元素的所有 ID 设置为 comment,DOM 可能会变得混乱。

I see a problem here:

    comment.id = "comment";

If you're setting all IDs of the comment elements to comment, the DOM may be getting confused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文