javascript - "Uncaught RefereceError: txtfile is not defined"

标签 javascript ajax html

我对此有点厌倦了。

我不知道为什么它一直告诉我文件未定义,因为它的声明和使用都在同一范围内。

<!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元素输入框。如果这是正确的,则可能值得为输入提供 inputtextid,然后使用 var inputarea = document.getElementById('inputtext') 调用它>。不过,如果您打算这样做,则值得将整个脚本放入 window.onload 回调中,以便在脚本运行时输入出现在页面上。

关于javascript - "Uncaught RefereceError: txtfile is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16598518/

相关文章:

PHP 脚本无法读取 ajax post 值以运行查询

javascript - 使用我的脚本从 XML 获取属性

html - 如果省略标签,Bootstrap 表单图标不在输入内

javascript - Typescript 体内参数变量的重复初始化

javascript - 根据选中的按钮做出决定

javascript - 如何检查此正则表达式中所有特殊字符的正斜杠?

javascript - 子菜单上方的水平菜单样式 "spill"

PHP 包含函数和 AJAX div 加载

javascript - 分别预隐藏按钮和图像作为按钮

html - 纯 CSS 上下文高亮