我试图以最简单的方式将新文本添加到现有文本中,在我的情况下,我只能修改段落元素内的脚本,但出现此错误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贡献1828条经验 获得超4个赞
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>
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 -->
添加回答
举报
0/150
提交
取消