如果我想永远不使用innerHTML,我还会使用什么?

编程入门 行业动态 更新时间:2024-10-24 20:15:01
本文介绍了如果我想永远不使用innerHTML,我还会使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的研究过程中,我偶然发现了Alex的这篇文章 Russel和Tim Scarfe: www.developer-x/content/i...l/default.html 有人认为永远不应该使用innerHTML。我想知道,如果 我希望BODY的所有内容都是一个字符串,除了通过innerHTML之外我怎么能得到 ?

In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: www.developer-x/content/i...l/default.html The case is made that innerHTML should never be used. I''m wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?

推荐答案

Jake Barnes写道: Jake Barnes wrote: 在我的研究过程中,我偶然发现了Alex的这篇文章.Russel和Tim Scarfe: http:// www.developer-x/content/i...l/default.html 表明永远不应该使用innerHTML。 这不是我从文章中理解的。我认为这表明 innerHTML有它的用途但不应该被滥用。 我想知道,如果我想要所有的BODY的内容为字符串,除了通过innerHTML之外我还能得到什么呢? In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: www.developer-x/content/i...l/default.html The case is made that innerHTML should never be used. That''s not what I understood from the article. I think it suggests that innerHTML has its uses but should not be abused. I''m wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?

有DOM 3加载和保存模块: " ...定义文档对象模型加载和保存级别3,一个 平台和语言中立的界面,允许程序和 脚本,用于将XML文档的内容动态加载到 DOM文档中,并将DOM文档序列化为XML文档; <网址:http://www.w3/TR/DOM-Level-3-LS/> 虽然它已达到推荐状态,但并不是很广泛 已实现。 您可以使用DOM迭代所有节点并构建自己的节点和属性的b ...但我希望这完全是 不切实际的除了琐碎的文档以外的任何东西,因为每个节点都有大量的属性,其中许多属性没有在 源HTML中明确设置。 - Rob

There is the DOM 3 Load and Save module: "...defines the Document Object Model Load and Save Level 3, a platform- and language-neutral interface that allows programs and scripts to dynamically load the content of an XML document into a DOM document and serialize a DOM document into an XML document;" <URL:www.w3/TR/DOM-Level-3-LS/> Although it has reached recommendation status, it isn''t widely implemented yet. You could iterate through all the nodes using DOM and build your own string of nodes and properties... but I expect that is totally impractical for anything other than trivial documents as each node has a large number of attributes, many of which are not explicitly set in the source HTML. -- Rob

Jake Barnes写道: Jake Barnes wrote: 在我的研究过程中,我偶然发现了这个文章由Alex Russel和Tim Scarfe撰写: www.developer-x/content/i...l/default.html 表明innerHTML永远不应该是用过的。 那将是一种愚蠢的态度。没有理由_never_使用任何环境提供的任何 功能。可能有很好的理由让b / b $ b更喜欢使用其他功能,任何决定使用任何给定的 功能都会产生后果,其中一些可能是负面的。但是每个 项目都有一个上下文,一些后果的意义各不相同 ,具体取决于使用功能的上下文。 最终你需要做的是做出明智的设计 决定适合你正在创作的环境。当你可以这样做时,你会明白何时以及为什么不使用innerHTML, 以及什么时候使用它。 我我想知道,如果我想把BODY的所有内容都作为一个字符串, 这个答案将取决于你认为你想要''内容 的BODY''作为一个字符串,你的意思是''BODY''和 你要在/ for中执行此操作的上下文。使用 - innerHTML - 可能有从来没有适合真正的任务开始。 除了通过innerHTML,我还能得到什么呢? In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: www.developer-x/content/i...l/default.html The case is made that innerHTML should never be used. That would be a stupid attitude. There is no reason to _never_ use any feature that any environment offers. There may be very good reasons for preferring to use other features, and any decision to use any given feature has consequences, some of which may be negative. But every project has a context, and some consequences vary in significance depending upon the context in which a feature is used. Ultimately what you need to be able to do is to make informed design decisions appropriate to the context in which you are authoring. When you can do that you will understand when and why not to use innerHTML, and when it is appropriate to use it. I''m wondering, If I wanted all the content of BODY as a string, For which the answer will depend on why you think you want the ''content of the BODY'' as a string, what you mean by the ''content of the BODY'' and what context you want to do this in/for. Using - innerHTML - may have never been appropriate for the real task to start with. how else could I get except through innerHTML?

如果所有其他方法都失败了,你可以自己序列化DOM(假设 浏览器环境暴露它,并且足够详细)。 Richard。

If all else fails you can serialize the DOM yourself (assuming the browser environment exposes it, and in sufficient detail). Richard.

RobG写道: Jake Barnes写道: Jake Barnes wrote:

[...]

[...]

我在想,如果我想把BODY的所有内容都作为字符串,我怎么能得到除了通过innerHTML? I''m wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?

有DOM 3加载和保存模块: " ...定义文档对象模型加载和保存级别3,一个平台和语言无关的界面,允许程序和脚本动态地将XML文档的内容加载到一个DO中M文档并将DOM文档序列化为XML文档; < URL:http://www.w3/TR/DOM-Level-3-LS/> 虽然它已达到推荐状态,但尚未广泛实施。

There is the DOM 3 Load and Save module: "...defines the Document Object Model Load and Save Level 3, a platform- and language-neutral interface that allows programs and scripts to dynamically load the content of an XML document into a DOM document and serialize a DOM document into an XML document;" <URL:www.w3/TR/DOM-Level-3-LS/> Although it has reached recommendation status, it isn''t widely implemented yet.

这里有一个小例子可行在最新的Gecko浏览器中(和 ,如果它实现了DOM 3的必要位,可能是IE 7但我对此表示怀疑): < script type =" text / javascript"> function showIt() { var s; if(''object''== typeof XMLSerializer && (s = new XMLSerializer())) { var pageSrc = s.serializeToString(document).replace(/ \< / g,''& LT;''); var oWin = window.open('''',''source'',''''); oWin.document。写( ''< title>页面来源< \ / title>< pre>'' + pageSrc + ''< \ / pre>'' ); oWin.document.close() }其他{ alert(''对不起,XMLSerializer()不支持''); } } < / script> < input type =" button" value ="显示页面来源" onclick =" showIt();"> - Rob

Here''s a small example that works in the latest Gecko browsers (and maybe IE 7 if it''s implemented the required bits of DOM 3 but I doubt it): <script type="text/javascript"> function showIt() { var s; if ( ''object'' == typeof XMLSerializer && (s = new XMLSerializer()) ) { var pageSrc = s.serializeToString(document).replace(/\</g,''&lt;''); var oWin = window.open('''',''source'',''''); oWin.document.write( ''<title>Source of page<\/title><pre>'' + pageSrc + ''<\/pre>'' ); oWin.document.close() } else { alert(''Sorry, XMLSerializer() not supported''); } } </script> <input type="button" value="Show page source" onclick="showIt();"> -- Rob

更多推荐

如果我想永远不使用innerHTML,我还会使用什么?

本文发布于:2023-11-06 01:30:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我想   我还   innerHTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!