简体   繁体   English

为什么此代码不警告文本文件中的文本? (javascript / html)

[英]Why is this code not alerting the text in the text file? (javascript/html)

Here is my code. 这是我的代码。 The problem is that the information isn't being alerted. 问题在于该信息未收到警报。 It worked in Js Fiddle but not on chrome/firefox/safari. 它在Js Fiddle中有效,但在chrome / firefox / safari中无效。 When I tried to use document.write to display the text in JsFiddle that didn't work either. 当我尝试使用document.write在JsFiddle中显示文本时,该文本也不起作用。 This code is giving me lots of problems. 这段代码给了我很多问题。

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
                    function readMultipleFiles(evt) {
            //Retrieve all the files from the FileList object
            var files = evt.target.files;
            if (files) {
                var r = new FileReader();
                for (var i = 0, f; f = files[i]; i++) {
                    r.onload = (function(f) {
                        return function(e) {
                            var contents = e.target.result;
                            alert(contents);
                        };
                    })(f);
                    r.readAsText(f);;
                }
            } else {
                alert("Failed to load files");
            }
        }
        document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);

    </script>
</head>
<body>
<input type="file" id="fileinput"/>

​</body>
</html>

try 尝试

window.onload = function() {
    document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
}

Instead of just 不只是

document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);

Edit: So yeah, the solution was here 编辑:是的,解决方案在这里

execute chrome with chrome.exe --disable-web-security for windows or google-chrome --disable-web-security for linux. 对于Windows,使用chrome.exe --disable-web-security执行chrome;对于Linux,则使用google-chrome --disable-web-security执行chrome。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:为什么我的代码什么都不提醒? - Javascript: Why is my code alerting to nothing? 使用Javascript将我的html输入限制为仅文本,并以其他方式提醒用户? - Using Javascript to limit my html input to only text and alerting the user otherwise? 为什么我的 javascript 代码没有更新 html 中的任何文本? - Why is my javascript code not updating any text from html? 带有文本框的JavaScript和html代码 - Javascript and html code with Text Box 为什么 Internet Explorer 调试器在代码和 iis 类型设置为 application/javascript 时将 javascript 文件识别为 text/html? - Why does Internet Explorer debugger identify javascript file as text/html when in the code and in iis the type is set as application/javascript? Javascript不是用C#背后的代码来警告数据库中的字符串变量值,而是警告整数变量,为什么? - Javascript not Alerting String Variable Value From Database in Code behind C#, but alerting integer variable, why? 功能提示错误的值/文本 - Function is alerting incorrect value/text 用jquery或其他HTML代码替换JavaScript文本 - Replace javascript text with jquery or other in HTML code 在生产代码中使用text / html javascript模板 - Using text/html javascript templates in production code 以html代码相同格式打印Javascript文本 - Print Javascript text in the same format text is in html code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM