簡體   English   中英

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

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

這是我的代碼。 問題在於該信息未收到警報。 它在Js Fiddle中有效,但在chrome / firefox / safari中無效。 當我嘗試使用document.write在JsFiddle中顯示文本時,該文本也不起作用。 這段代碼給了我很多問題。

<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>

嘗試

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

不只是

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

編輯:是的,解決方案在這里

對於Windows,使用chrome.exe --disable-web-security執行chrome;對於Linux,則使用google-chrome --disable-web-security執行chrome。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM