尝试添加 HTML 节点但它没有出现

发布于 2024-09-25 06:01:48 字数 1574 浏览 4 评论 0原文

我有以下 javascript 函数:

    function addConfirmLine(number, strItem, strValue) {
        var confirmLine = document.getElementById("divConfirmation").appendChild(document.createElement("div"));
        confirmLine.id = "divConfirmLine" + number;

        var confirmItem = confirmLine.appendChild(document.createElement("div"));
        confirmItem.className = "confirmItem";
        confirmItem.nodeValue = strItem;

        var confirmValue = confirmLine.appendChild(document.createElement("div"));
        confirmValue.className = "confirmValue";
        confirmValue.nodeValue = strValue;
    }

和一个像这样的 div

<div id="divConfirmation">
    <div class="checkHead">
        Check the following details.  Click "Prev" to make corrections.  Click "Upload" to process and upload the sermon.
    </div>
</div>

目的是最终得到这样的结果:

<div id="divConfirmation">
    <div class="checkHead">
        Check the following details.  Click "Prev" to make corrections.  Click "Upload" to process and upload the sermon.
    </div>
    <div id="divConfirmLine1">
        <div class="confirmItem">Item1</div>
        <div class="confirmValue">Value1</div>
    </div>
    <div id="divConfirmLine2">
        <div class="confirmItem">Item2</div>
        <div class="confirmValue">Value2</div>
    </div>
</div>

问题是它不起作用。新的 div 没有出现,并且我没有收到任何错误。我做错了什么?

I've got the following javascript function:

    function addConfirmLine(number, strItem, strValue) {
        var confirmLine = document.getElementById("divConfirmation").appendChild(document.createElement("div"));
        confirmLine.id = "divConfirmLine" + number;

        var confirmItem = confirmLine.appendChild(document.createElement("div"));
        confirmItem.className = "confirmItem";
        confirmItem.nodeValue = strItem;

        var confirmValue = confirmLine.appendChild(document.createElement("div"));
        confirmValue.className = "confirmValue";
        confirmValue.nodeValue = strValue;
    }

and a div like this

<div id="divConfirmation">
    <div class="checkHead">
        Check the following details.  Click "Prev" to make corrections.  Click "Upload" to process and upload the sermon.
    </div>
</div>

The intent is to end up with something like this:

<div id="divConfirmation">
    <div class="checkHead">
        Check the following details.  Click "Prev" to make corrections.  Click "Upload" to process and upload the sermon.
    </div>
    <div id="divConfirmLine1">
        <div class="confirmItem">Item1</div>
        <div class="confirmValue">Value1</div>
    </div>
    <div id="divConfirmLine2">
        <div class="confirmItem">Item2</div>
        <div class="confirmValue">Value2</div>
    </div>
</div>

Problem is it doesn't work. The new divs don't appear, and I don't get any errors. What am I doing wrong?

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

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

发布评论

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

评论(1

给妤﹃绝世温柔 2024-10-02 06:01:48

元素的 nodeValue 始终为 null。您想要添加文本节点:

confirmItem.appendChild(document.createTextNode(strItem));
confirmValue.appendChild(document.createTextNode(strValue));

The nodeValue of an Element is always null. You want to add text nodes:

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