无法在“Node”上执行“appendChild”:参数 1 不是“Node”类型

我试图以最简单的方式向现有文本添加新文本,在我的情况下,我只能修改段落元素内的脚本,但我收到此错误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 -->


潇潇雨雨
浏览 64回答 1
1回答

泛舟湖上清波郎朗

textNode您应该使用createTextNode()方法创建文本,例如,const textNode = document.createTextNode(" and this is the new text added");并将创建的节点作为参数传递给appendChild,例如document.currentScript.parentNode.appendChild(textNode);修改后的片段如下:<!-- 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 -->
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5