我对此有点厌倦了。
我不知道为什么它一直告诉我文件未定义,因为它的声明和使用都在同一范围内。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<script type="text/javascript">
var txtFile = new XMLHttpRequest();
var inputarea = document.inputtext;
txtFile.open("GET", "start.txt", true);
txtFile.onreadystatechange = function() {
// Makes sure the document is ready to parse.
if(txtFile.readyState === 4) {
// Makes sure it's found the file.
if(txtFile.status === 200) {
allText = txtFile.responseText;
// Will separate each line into an array
lines = txtFile.responseText.split("\n");
for(i = 0; i < lines.length; i++) {
var s = lines[i];
if(s.indexOf("nextpage") > -1) {
// Line is there
} else {
// Line is not there
inputarea.value += s;
}
}
}
}
}
txtfile.send();
</script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<textarea name="inputtext" rows="4" cols="20" readonly="readonly">
</textarea>
<div>TODO write content</div>
</body>
</html>
编辑
问题已解决,但我现在收到另一个错误:
未捕获类型错误:无法读取未定义的属性“值”(00:27:29:739 | 错误,javascript)
在 txtFile.onreadystatechange (public_html/index.html:29:42)
最佳答案
检查拼写 txtfile.send()
应为 txtFile.send()
,注意 f
的大小写
更新第二个错误
错误是因为document.inputtext
未定义,我假设它是一个html元素输入框。如果这是正确的,则可能值得为输入提供 inputtext
的 id
,然后使用 var inputarea = document.getElementById('inputtext')
调用它>。不过,如果您打算这样做,则值得将整个脚本放入 window.onload
回调中,以便在脚本运行时输入出现在页面上。
关于javascript - "Uncaught RefereceError: txtfile is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16598518/