我有这样的 HTML 内容:
some of the string content <font color=blue>Test content <BR><BR><BR>
<DIV id='idOfTheDiv'>
some more goes here
<P>Test Content</P>
</DIV>
</font>
我想删除 div 而不删除其内容,因此结果数据应如下所示
some of the string content `<font color=blue>Test content <BR><BR><BR>`
some more goes here
<P>Test Content</P>
</font>
请注意,我不想删除 div 的内容,也不想添加任何不需要的 HTML 元素只是为了删除 div。我尝试了各种技术,但目前都不起作用。
我尝试用这个替换innerHTML,但没有成功。我不能使用replaceChild,因为
<DIV id='idOfTheDiv'>
some more goes here
<P>Test Content</P>
</DIV>
是文本加 HTML 的组合,因此 CreateTextNode 在这里不起作用,因为它将所有 HTML 更改为纯文本。
请提出建议。非常感谢..
最佳答案
循环 div 内的元素(使用
childNodes
,因为它还包含文本节点,而children
则不包含)。使用
insertBefore
将元素一一放置在 div 之前.使用
removeChild
删除 div .
关于javascript - 不使用jquery替换HTML内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22267667/