猿问

无法在“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 -->


叮当猫咪
浏览 254回答 1
1回答

明月笑刀无情

textNode您应该使用createTextNode()方法创建文本,例如,const textNode = document.createTextNode(" and this is the new text added");并将创建的节点作为参数传递给 appendChild 之类的,document.currentScript.parentNode.appendChild(textNode);修改后的片段如 sollows,<!-- Many elements above this --><p>&nbsp; This a part of the text&nbsp; <script>&nbsp; &nbsp; const textNode = document.createTextNode(" and this is the new text added");&nbsp; &nbsp; document.currentScript.parentNode.appendChild(textNode);&nbsp; </script></p><!-- Many elements under this -->
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答