問(wèn)題描述
這個(gè):
function myFunction()
{
document.write("sup");
}
在 html 中調(diào)用如下:
called in html like:
<div id="myDiv">
<script>myFunction();</script>
</div>t
將字符串 sup
添加到 myDiv
div 元素.這正是我想要的.但是,這個(gè):
adds a string sup
to the myDiv
div element. Which is what I want, exactly. However, this:
function loadFile(uri)
{
var r = new XMLHttpRequest();
document.write("trying to open: " + uri);
r.open('GET', uri, true);
r.send(null);
r.onreadystatechange = function()
{
if (r.readyState == 4)
{
myFunction();
}
}
}
function myFunction()
{
document.write("sup");
}
這樣稱呼:
<div id="myDiv">
<script>loadFile("filename.txt");</script>
</div>
似乎覆蓋了我的整個(gè) html 文件.IE.當(dāng)我在 Firefox 中運(yùn)行它時(shí),它只顯示字符串 sup
(這是頁(yè)面的全部?jī)?nèi)容),但頁(yè)面似乎仍在加載(FF 的加載圖標(biāo)仍然存在動(dòng)畫,顯然是無(wú)限的).
seems to be overwriting my whole html file. I.e. when I run it in Firefox it shows me only the string sup
(that's the whole content of the page) but the page seems to be still loading (the loading icon of FF is still there animating, apparently infinitely).
首先,這將僅在本地離線使用,作為一種快速便捷的數(shù)據(jù)呈現(xiàn)方式(使用 html+js 和 Web 瀏覽器而不是純文本文件).我想要的是加載一個(gè)本地文本文件,然后將它的一些內(nèi)容作為 html 頁(yè)面的一部分.與我的第一個(gè)示例相同,但首先加載文本文件.
First of all, this is going to be used only locally, offline, as a fast and handy way of presenting data (using html+js and web browser instead of plain text file). What I want is to load a local text file and then put some of its content as a part of the html page. The same as in my first example but with loading the text file first.
推薦答案
問(wèn)題是當(dāng)你在文檔加載后運(yùn)行 document.write 時(shí),它??會(huì)覆蓋整個(gè)文檔.如果在此之前運(yùn)行,它不會(huì)覆蓋它.
The issue is that when you run document.write after the document has loaded, it overwrites the entire document. If it is run before that, it does not overwrite it.
您要做的是設(shè)置特定元素的innerHtml,例如:
What you want to do is set the innerHtml of a specific element, something like:
document.getElementById("myDiv").innerHTML="Sup";
這篇關(guān)于document.write() 覆蓋文檔?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!