因此,由于与其他方法相比用途有限,因此最好精通其他方法.Why is it generally not considered good practice to use document.write in Javascript? I know it is not the most elegant or best way to do it, but are there any true errors with it? Is it ever ok? And in what case would it be better than inner.HTMLI understand that it will rewrite the document, if used after the loading. However, should I be using innerHTML even if I am trying to rewrite the document? If so, why?I know some have asked if I even looked at the related questions before posting; I did. But I did not find anything satisfactory. 解决方案 When used correctly, it's fine, but of very limited use.You can't use it in XHTML, but I would recommend not using XHTML anyway unless you have a really, really good reason to.The problem comes when you call document.write after the initial page load is complete. During page load is fine, but afterward, it causes an implicit document.open, which clears the document from the window and starts fresh.Using document.write also means you can't demand-load your scripts or use the async or defer attributes on your script elements.Finally, I usually recommend putting your JavaScript in a separate (single) JavaScript file at the very end of the document (just before the closing </body> tag). If you want to use document.write to output to the page, you'd have to include that script file higher up or use code directly within the HTML, neither both of which I recommend against.So since it's of such limited use compared with other methods, it's best to get proficient with the other methods. 这篇关于为什么不使用document.write?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 13:57