簡體   English   中英

在文本節點之前插入文本節點而不在 Chrome 中合並

[英]Insert Text Node before Text Node without merging in Chrome

我想在另一個文本節點之前插入一個文本節點,如下所示:

<p id="4">text<b>!</b></p>
document.getElementById("4").childNodes[0].before("more ");
<p id="4">more text<b>!</b></p>

這很好用,除了在 Chrome 中它會合並文本節點:

<p>[#text more text]<b>!</b></p>

在 Firefox 中,文本節點不會合並:

<p>[#text more ][#text text]<b>!</b></p>

如何讓 Chrome 像 Firefox 一樣運行? 我已經確定它與before()方法無關,因為我嘗試了其他插入文本的方法。 我對 jQuery 解決方案不感興趣。

此解決方案與所有瀏覽器兼容

 let element = document.getElementById("4"); element.innerText = "more " + element.innerText;
 <p id="4">text</p>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM