简体   繁体   English

javascript document.write

[英]javascript document.write

I've been looking on Google and I searched on SO for the answer, but I can't find anything. 我一直在寻找谷歌,我搜索了SO的答案,但我找不到任何东西。

The document.write function seems pretty useless because it replaces the previous contents. document.write函数似乎没用,因为它取代了以前的内容。 Does it accept any arguments, or is there a legitimate use for it? 它是否接受任何论据,或者它是否合法使用?

Documentation can be found at MDN . 可在MDN上找到文档 It only replaces the content if you call it after the document was parsed. 如果在解析文档调用它它只会替换内容。

If it is called during parsing, then the content is just inserted. 如果在解析期间调用它,则只插入内容。 Quite a few scripts use this, so that you just have to copy and paste them in your HTML where you want to have the output, without any further actions required from your side. 相当多的脚本使用它,因此您只需将它们复制并粘贴到您想要输出的HTML中,而无需您身边的任何进一步操作。

Its usage is not undisputed though, because of what you already mentioned: If you use it at the wrong time, all the content is erased. 它的用法并不是无可争议的,因为你已经提到过:如果你在错误的时间使用它,所有的内容都会被删除。 Furthermore, it is not available in XHTML documents. 此外,它在XHTML文档中不可用。

But IMO it's the same with many other things: If you are aware of the difficulties/drawbacks and know when to use it, then it's ok to use it. 但IMO与其他许多东西是一样的:如果你意识到困难/缺点并知道何时使用它,那么可以使用它。

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

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