【问题标题】:Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node [duplicate]无法在“Node”上执行“appendChild”:参数 1 不是“Node”类型 [重复]
【发布时间】:2020-04-01 12:10:38
【问题描述】:

我试图以最简单的方式将新文本添加到现有文本中,在我的情况下,我只能修改段落元素内的脚本,但我收到此错误 Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node。如何以尽可能短的代码使其工作?

<!-- Many elements above this -->
<p>
  This a part of the text
  <script>
    document.currentScript.parentNode.appendChild(" and this is the new text added");
  </script>
</p>
<!-- Many elements under this -->

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您应该使用createTextNode() 方法将文本创建为textNode

    const textNode = document.createTextNode(" and this is the new text added");
    

    并将创建的节点作为参数传递给 appendChild 之类的,

    document.currentScript.parentNode.appendChild(textNode);
    

    修改后的sn-p为sollows,

    <!-- Many elements above this -->
    <p>
      This a part of the text
      <script>
        const textNode = document.createTextNode(" and this is the new text added");
        document.currentScript.parentNode.appendChild(textNode);
      </script>
    </p>
    <!-- Many elements under this -->

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 2019-08-16
      • 2022-11-21
      • 1970-01-01
      相关资源
      最近更新 更多