我懂文件。写作被认为是坏习惯;我希望整理一份理由清单,提交给第三方供应商,告诉他们为什么不应该使用文档。编写分析代码的实现。

请附上申领文件的理由。下面是一个坏习惯。


当前回答

文件的缺点。写作主要取决于这3个因素:

)实现

write()主要用于在需要内容时立即将内容写入屏幕。这意味着它发生在任何地方,要么在JavaScript文件中,要么在HTML文件中的脚本标记中。由于script标记被放置在这样的HTML文件中的任何地方,在与网页中的HTML交织在一起的脚本块中使用document.write()语句是一个坏主意。

b)呈现

Well designed code in general will take any dynamically generated content, store it in memory, keep manipulating it as it passes through the code before it finally gets spit out to the screen. So to reiterate the last point in the preceding section, rendering content in-place may render faster than other content that may be relied upon, but it may not be available to the other code that in turn requires the content to be rendered for processing. To solve this dilemma we need to get rid of the document.write() and implement it the right way.

c)不可能的操作

一旦写好了,就结束了。如果不进入DOM,我们就不能返回去操作它。

其他回答

文件的缺点。写作主要取决于这3个因素:

)实现

write()主要用于在需要内容时立即将内容写入屏幕。这意味着它发生在任何地方,要么在JavaScript文件中,要么在HTML文件中的脚本标记中。由于script标记被放置在这样的HTML文件中的任何地方,在与网页中的HTML交织在一起的脚本块中使用document.write()语句是一个坏主意。

b)呈现

Well designed code in general will take any dynamically generated content, store it in memory, keep manipulating it as it passes through the code before it finally gets spit out to the screen. So to reiterate the last point in the preceding section, rendering content in-place may render faster than other content that may be relied upon, but it may not be available to the other code that in turn requires the content to be rendered for processing. To solve this dilemma we need to get rid of the document.write() and implement it the right way.

c)不可能的操作

一旦写好了,就结束了。如果不进入DOM,我们就不能返回去操作它。

一个简单的原因为什么文件。写是一个不好的做法,因为你无法想出一个你找不到更好的替代方案的场景。 另一个原因是您正在处理字符串而不是对象(这是非常原始的)。 它只附加到文档中。 它没有MVC(模型-视图-控制器)模式的美感。 使用ajax+jQuery或angularJS来呈现动态内容要强大得多。

Chrome浏览器可能阻止文档。编写在某些情况下插入脚本的代码。当这种情况发生时,它将在控制台中显示以下警告:

一个解析器阻塞,跨源脚本,…,通过 document . write。这可能会被浏览器阻止,如果设备 网络连通性差。

引用:

developers.google.com上的这篇文章将详细介绍。 https://www.chromestatus.com/feature/5718547946799104

我能想到的是:

文档。写需要用在页面加载或正文加载上。因此,如果您想在任何其他时间使用脚本来更新您的页面内容文档。写作几乎毫无用处。 技术文档。write只更新HTML页面,不更新XHTML/XML。IE似乎对这个事实很宽容,但其他浏览器不会。

http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

一些比较严重的问题:

document.write (henceforth DW) does not work in XHTML DW does not directly modify the DOM, preventing further manipulation (trying to find evidence of this, but it's at best situational) DW executed after the page has finished loading will overwrite the page, or write a new page, or not work DW executes where encountered: it cannot inject at a given node point DW is effectively writing serialised text which is not the way the DOM works conceptually, and is an easy way to create bugs (.innerHTML has the same problem)

最好使用安全且DOM友好的DOM操作方法