为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇潇雨雨 2023-10-24 21:19:20
我试图以最简单的方式向现有文本添加新文本,在我的情况下,我只能修改段落元素内的脚本,但我收到此错误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 -->
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

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 -->


查看完整回答
反对 回复 2023-10-24
  • 1 回答
  • 0 关注
  • 62 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信