2014-01-29 23 views
-1

問題在下面。目標是在html中顯示txt文件中的內容。 頭的HTML文件:html代碼總是顯示一個txt文件內的消息

<script type="text/javascript"> 
function loadXMLDoc() { 
var xmlhttp; 
if (window.XMLHttpRequest) { 
xmlhttp=new XMLHttpRequest(); 
} else { 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() { 
if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
} 
} 
xmlhttp.open("GET","news.txt",true); 
xmlhttp.send(); 
} 
</script> 

顯示部分體:

<div class="span-3" id="right-side"> <div id="myDiv">Your text here! 
    </div><br><br><br><button type="button" onclick="loadXMLDoc()">Click Here!</button> 

但問題,它需要用戶干預。我不想要按鈕,文本需要始終顯示。我應該做什麼差異?

+0

這個怎麼樣? '

' –

+0

它不適合我嗎?我聽說我需要解析html。怎麼做? – mreccentric

回答

-1

你的身體標記更改爲:

<body onLoad="loadXMLDoc()"> 
+0

您的意思是將第二組陳述替換爲您所說的一個陳述? – mreccentric

+0

不起作用。不知道爲什麼。 – mreccentric

+0

你只用我建議的替換你的身體標記。沒有其他的。如果你願意,你可以刪除按鈕。 – Alex

0

關於你提到的其他問題, 你的代碼應該是這個樣子:

<!DOCTYPE html> 
<html> 
<head> 
    <title>title</title> 
</head> 
<body onLoad="loadXMLDoc()"> 
<div class="span-3" id="right-side"> 
    <div id="myDiv"> 
     Your text here! 
    </div> 
</body> 
</html> 
<script type="text/javascript"> 
function loadXMLDoc() { 
    var xmlhttp; 
    if (window.XMLHttpRequest) { 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      document.getElementById("myDiv").innerHTML = xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","news.txt",true); 
    xmlhttp.send(); 
} 
</script> 

你可以閱讀更多關於onLoad事件Here

+0

這適用於我的需求。任何想法如何我可以選擇從底部到頂部的文本? – mreccentric

+0

文本 Alex

+0

我想選取「news.txt」中的數據。想要爲您的答案添加讚許,但因爲我是新的,所以無法進行。 – mreccentric

0

with marquee:

<!DOCTYPE html> 
<html> 
<head> 
    <title>title</title> 
</head> 
<body onLoad="loadXMLDoc()"> 
<div class="span-3" id="right-side"> 
    <div > 
     <marquee direction="up" id="myDiv"></marquee> 
    </div> 
</body> 
</html> 
<script type="text/javascript"> 
function loadXMLDoc() { 
    var xmlhttp; 
    if (window.XMLHttpRequest) { 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      document.getElementById("myDiv").innerHTML = xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","news.txt",true); 
    xmlhttp.send(); 
} 

如果你想打破顯示,只需添加您的.txt文件中 <br>標記線

...

+0

非常感謝。我會檢查這個工作,然後回來。對不起,來晚了。 – mreccentric

+0

不能...選框不顯示。我的意思是,沒有文字顯示。 – mreccentric

相關問題