我試圖以最簡(jiǎn)單的方式向現(xiàn)有文本添加新文本,在我的情況下,我只能修改段落元素內(nèi)的腳本,但我收到此錯(cuò)誤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貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
textNode
您應(yīng)該使用createTextNode()方法創(chuàng)建文本,例如,
const?textNode?=?document.createTextNode("?and?this?is?the?new?text?added");
并將創(chuàng)建的節(jié)點(diǎn)作為參數(shù)傳遞給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 -->
- 1 回答
- 0 關(guān)注
- 193 瀏覽
添加回答
舉報(bào)
0/150
提交
取消