简体   繁体   English

innerHTML替代检索页面内容?

[英]innerHTML alternative for retrieving contents of page?

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. 我当前正在使用innerHTML来检索HTML元素的内容,并且我发现在某些浏览器中,它不能完全返回源中的内容。

For example, using innerHTML in Firefox on the following line: 例如,在以下行上使用Firefox中的innerHTML:

<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. 在IE中,它返回原始字符串,带有两个关闭的强标签。 I'm assuming in most cases it's not a problem (and may be a benefit) that Firefox cleans up the incorrect code. 我假设在大多数情况下,Firefox清理不正确的代码不是问题(可能是一个好处)。 However, for what I'm trying to accomplish, I need the exact code as it appears in the original HTML source. 但是,对于我要完成的工作,我需要显示在原始HTML源代码中的确切代码。

Is this at all possible? 这是可能吗? Is there another Javascript function I can us? 我可以使用另一个Javascript函数吗?

I don't think you can receive incorrect HTML code in modern browsers. 我认为您不会在现代浏览器中收到不正确的HTML代码。 And it's right behaviour, because you don't have source of dynamicly generated HTML. 这是正确的行为,因为您没有动态生成的HTML的来源。 For example Firefox' innerHTML returns part of DOM tree represented in string. 例如,Firefox的innerHTML返回以字符串表示的DOM树的一部分。 Not an HTML source. 不是HTML来源。 And this is not a problem because second </strong> tag is ignored by the browser anyway. 这不是问题,因为浏览器始终会忽略第二个</strong>标记。

innerHTML is generated not from the actual source of the document ie. innerHTML不是从文档的实际来源生成的。 the HTML file but is derived from the DOM object that is rendered by the browser. HTML文件,但派生自浏览器呈现的DOM对象。 So if IE somehow shows you incorrect HTML code then it's probably some kind of bug. 因此,如果IE以某种方式向您显示不正确的HTML代码,则可能是某种错误。 There is no such method to retrieve the invalid HTML code in every browser. 没有这样的方法来在每个浏览器中检索无效的HTML代码。

You can't in general get the original invalid HTML for the reasons Ivan and Andris said. 由于Ivan和Andris所说的原因,您通常无法获得原始的无效HTML。

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. IE也像Firefox一样“固定”您的代码,尽管您不会在序列化中注意到这种方式,方法是创建一个带有tagName /strong的Element节点,以对应于伪造的最终标签。 There is no guarantee at all that IE will happen to preserve other invalid markup structures through a parse/serialise cycle. 根本无法保证IE将通过解析/序列化周期保留其他无效的标记结构。

In fact even for valid code the output of innerHTML won't be exactly the same as the input. 实际上,即使对于有效的代码, innerHTML的输出也不会与输入完全相同。 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. 不维护属性顺序,不维护tagName大小写(IE给您<STRONG> ),空格丢失了各个地方,不维护实体引用,依此类推。 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. 如果“需要精确的代码”,则必须保留精确的代码副本,例如,在相关内容之后编写的<script>块中的JavaScript变量中。

If you don't need the HTML to render (eg, 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. 如果不需要HTML呈现(例如,将其用作JS模板之类的东西),则可以将其放在文本区域中,并使用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. 除此之外,浏览器可以决定如何解释HTML,它只会返回您的解释,而不是原始的解释。

innerTEXT ? innerTEXT? or does that have the same eeffect? 或具有相同的效果?

You must use innerXML property. 您必须使用innerXML属性。 It does exactly what you want to achieve. 它正是您想要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM