不使用innerHTML获取innerHTML
我知道 innerHTML
应该很讨厌(我们不要开始争论。我们正试图在我的工作中逐步淘汰它),但我需要能够获取某些 HTML 的纯文本版本。现在我有这些行...
bodycontents = document.getElementById("renderzone").innerHTML;
但我想知道如何在没有 innerHTML
的情况下获得相同的结果。我并不害怕 DOM 操作(我已经做过很多了),所以请随意在你的答案中尽可能地技术化。
I know innerHTML
is supposed to be icky (and let's not start a debate. We're trying to phase it out at my work), but I need to be able to get the plaintext version of some HTML. Right now I have the lines...
bodycontents = document.getElementById("renderzone").innerHTML;
But I am wondering exactly how I would, say, get the same result WITHOUT innerHTML
. I am not afraid of DOM manipulation (I've done a ton of it), so please feel free to be as technical in your answer as you need to be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在不使用
element.innerHTML
属性的情况下检索元素内部 HTML 内容听起来像是一项学术追求,不太可能有简单的解决方案。基于 DOM 的方法需要递归地检索所有子节点并附加其文本内容并序列化元素名称和属性。例如(在 JavaScript/DOM 伪代码中):
Retrieving an elements inner HTML content without using the
element.innerHTML
attribute sounds like an academic pursuit that isn't likely to have a simple solution.A DOM based approach would require something like recursively walking to retrieve all child nodes and append their text content and serialize the element name and attributes. For example (in JavaScript/DOM pseudo-code):
在现代浏览器上,
XMLSerializer
可以帮助您:On modern browser the
XMLSerializer
may help you:我想你想要这个:
I think you want this: