scala document.write 在 html 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7574943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:29:47  来源:igfitidea点击:

document.write not working in html

javascriptjqueryscalalift

提问by oshai

I am try to write inside html document. this is my javascript code:

我尝试在 html 文档中写入。这是我的 javascript 代码:

<script type="text/javascript">document.write("Hello World!")</script>

I am working with chrome and get the following error:

我正在使用 chrome 并收到以下错误:

Uncaught TypeError: Object # has no method 'write'

未捕获的类型错误:对象 # 没有方法“写入”

I tried alertmethod and it worked.

我尝试了alert方法,它奏效了。

EDIT: this is part of a project in scala/lift that also uses jquery if that may hint something. I suspect document object is redefined. is there a way to know that / to access the original one?

编辑:这是 scala/lift 中的一个项目的一部分,如果这可能暗示某些事情,它也使用 jquery。我怀疑文档对象被重新定义。有没有办法知道/访问原始的?

回答by cmanteuffel

I had a similar problem, trying to embed the google maps api in my lift application. The script also uses document.write to load external libraries. The Google Chrome console stated that there is no function document.write.

我有一个类似的问题,试图在我的电梯应用程序中嵌入谷歌地图 api。该脚本还使用 document.write 加载外部库。谷歌浏览器控制台声明没有函数document.write。

The problem seems to be that XHTML does not allow document.write. ( http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite) A solution could be to change the mime/type of your documents, e.g. by adding the following line to your Boot.scala

问题似乎是 XHTML 不允许 document.write。( http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite) 解决方案可能是更改文档的 mime/type,例如通过将以下行添加到 Boot.scala

LiftRules.useXhtmlMimeType = false

LiftRules.useXhtmlMimeType = false

More solutions are described in the link below http://scala-programming-language.1934581.n4.nabble.com/Google-Maps-API-V2-amp-V3-Ajax-Loading-td1981862.html

以下链接中描述了更多解决方案 http://scala-programming-language.1934581.n4.nabble.com/Google-Maps-API-V2-amp-V3-Ajax-Loading-td1981862.html

回答by ilivewithian

Do you have another variable called document?

你有另一个叫做文档的变量吗?

回答by Michael D. Irizarry

Did you try

你试过了吗

<script type="text/javascript">window.document.write("Hello World!")</script>

回答by oshai

I am not sure why this happened, maybe its related to the phase the page load was on. as a workaround I added a div with id hellowworlddivand the following worked:

我不确定为什么会发生这种情况,可能与页面加载所处的阶段有关。作为一种解决方法,我添加了一个带有 id 的 divhellowworlddiv并且以下操作有效:

<script type="text/javascript">document.getElementById("hellowworlddiv").innerHTML = "Hello World!"</script>