用于检索页面内容的innerHTML替代方案?

发布于 2024-08-17 22:51:38 字数 449 浏览 5 评论 0原文

我目前正在使用innerHTML 来检索HTML 元素的内容,并且我发现在某些浏览器中它不会准确返回源中的内容。

例如,在 Firefox 中的以下行中使用innerHTML:

<div id="test"><strong>Bold text</strong></strong></div>

将返回:

<strong>Bold text</strong>

在 IE 中,它返回原始字符串,并带有两个结束强标记。我假设在大多数情况下,Firefox 清理不正确的代码不是问题(而且可能是一个好处)。然而,对于我想要完成的任务,我需要原始 HTML 源代码中显示的确切代码。

这有可能吗?我还可以使用其他 Javascript 函数吗?

I'm currently using innerHTML to retrieve the contents of an HTML element and I've discovered that in some browsers it doesn't return exactly what is in the source.

For example, using innerHTML in Firefox on the following line:

<div id="test"><strong>Bold text</strong></strong></div>

Will return:

<strong>Bold text</strong>

In IE, it returns the original string, with two closing strong tags. I'm assuming in most cases it's not a problem (and may be a benefit) that Firefox cleans up the incorrect code. However, for what I'm trying to accomplish, I need the exact code as it appears in the original HTML source.

Is this at all possible? Is there another Javascript function I can us?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

饮惑 2024-08-24 22:51:38

我认为您不会在现代浏览器中收到不正确的 HTML 代码。这是正确的行为,因为您没有动态生成的 HTML 源。例如,Firefox 的 innerHTML 返回以字符串表示的 DOM 树 的一部分。不是 HTML 源。这不是问题,因为浏览器无论如何都会忽略第二个 标记。

I don't think you can receive incorrect HTML code in modern browsers. And it's right behaviour, because you don't have source of dynamicly generated HTML. For example Firefox' innerHTML returns part of DOM tree represented in string. Not an HTML source. And this is not a problem because second </strong> tag is ignored by the browser anyway.

清秋悲枫 2024-08-24 22:51:38

innerHTML 不是从文档的实际源生成的,即。 HTML 文件,但派生自浏览器呈现的 DOM 对象。因此,如果 IE 以某种方式向您显示不正确的 HTML 代码,那么它可能是某种错误。没有这样的方法可以在每个浏览器中检索无效的 HTML 代码。

innerHTML is generated not from the actual source of the document ie. the HTML file but is derived from the DOM object that is rendered by the browser. So if IE somehow shows you incorrect HTML code then it's probably some kind of bug. There is no such method to retrieve the invalid HTML code in every browser.

╰沐子 2024-08-24 22:51:38

由于 Ivan 和 Andris 所说的原因,您通常无法获得原始的无效 HTML。

IE 也像 Firefox 一样“修复”您的代码,尽管您在序列化时没有注意到,方法是使用 tagName /strong 创建一个 Element 节点来对应伪造的结束标记。根本无法保证 IE 会通过解析/序列化周期保留其他无效的标记结构。

事实上,即使对于有效的代码,innerHTML 的输出也不会与输入完全相同。不维护属性顺序,不维护 tagName 大小写(IE 为您提供 ),不同位置的空格丢失,不维护实体引用,等等。如果您“需要准确的代码”,则必须保留准确代码的副本,例如在相关内容之后编写的

You can't in general get the original invalid HTML for the reasons Ivan and Andris said.

IE is also “fixing” your code just like Firefox does, albeit in a way you don't notice on serialisation, by creating an Element node with the tagName /strong to correspond to the bogus end-tag. There is no guarantee at all that IE will happen to preserve other invalid markup structures through a parse/serialise cycle.

In fact even for valid code the output of innerHTML won't be exactly the same as the input. Attribute order isn't maintained, tagName case isn't maintained (IE gives you <STRONG>), whitespace is various places is lost, entity references aren't maintained, and so on. If you “need the exact code”, you will have to keep a copy of the exact code, for example in a JavaScript variable in a <script> block written after the content in question.

作妖 2024-08-24 22:51:38

如果您不需要 HTML 来呈现(例如,您将使用它作为 JS 模板或其他东西),您可以将其放入文本区域并使用 innerHTML 检索内容。

<textarea id="myTemplate"><div id="test"><strong>Bold text</strong></strong></div></textarea>

然后:

$('#myTemplate').html() === '<div id="test"><strong>Bold text</strong></strong></div>'

除此之外,浏览器可以决定如何解释 HTML,它只会返回它的解释,而不是原始内容。

If you don't need the HTML to render (e.g., you're going to use it as a JS template or something) you can put it in a textarea and retrieve the contents with innerHTML.

<textarea id="myTemplate"><div id="test"><strong>Bold text</strong></strong></div></textarea>

And then:

$('#myTemplate').html() === '<div id="test"><strong>Bold text</strong></strong></div>'

Other than that, the browser gets to decide how to interpret the HTML and it will only return you it's interpretation, not the original.

浅暮の光 2024-08-24 22:51:38

内部文本?或者有同样的效果吗?

innerTEXT ? or does that have the same eeffect?

南城旧梦 2024-08-24 22:51:38

您必须使用 innerXML 属性。它正是您想要实现的目标。

You must use innerXML property. It does exactly what you want to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文