文本节点方法replaceWholeText()
已过时,还有哪些其他方法可以做到这一点?
最佳答案
您可以使用parentNode.normalize()
方法来删除所有空的和相邻的TextNode(即您将只有一个由wholeText
组成的TextNode),然后将合并的TextNode的textContent设置为替换值:
const para = document.querySelector('p');
para.appendChild(new Text('baz')); // would remain if not normalized
console.log('before normalizing');
console.log('textContent: ', para.firstChild.textContent);
console.log('wholeText: ',para.firstChild.wholeText);
para.normalize();
console.log('after normalizing');
console.log('textContent: ', para.firstChild.textContent);
console.log('wholeText: ',para.firstChild.wholeText);
// now we can set the wholeText to whatever we want.
para.firstChild.textContent = 'Hello world';
<p>
foo bar
</p>
关于javascript - .replaceWholeText()方法已过时,还有哪些其他方法可以做到这一点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47746136/