Html 非常简单的 javascript 根本不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16927447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:04:53  来源:igfitidea点击:

Very simple javascript doesn't work at all

javascripthtmldomjavascript-events

提问by TeeJay

my javascript in .php (static website) file doesn't work. I use there 3 other scripts and they do work, but not this one.

我的 .php(静态网站)文件中的 javascript 不起作用。我使用了其他 3 个脚本,它们确实有效,但不是这个。

The situation is simple

情况很简单

The javascript is...

javascript是...

var myFile = document.getElementById('myFile');

myFile.addEventListener('change', function() {

  alert(this.files[0].size); 
});

and the HTML code in the PHP file is (stripped)

并且 PHP 文件中的 HTML 代码被(剥离)

<form onsubmit="return anotherfunction();" action="sendForm.php" enctype="multipart/form-data" method="post">    
  <table>
    <tr>
        <td>Attach an image:</td>
        <td> <input type="file" name="priloha[]" accept="image/*" /></td>
    </tr>
  </table>
</form>

And the javascript does nothing, it acts like it doesn't exist. I tried to use the JS in the header, below the header, in the body, in the TD of the input... I also tried using it in external .js file, nothing, doesn't work at all. I tried changing "change" to "click", didn't work neither. I tried to do this all day and I can't figure out what's wrong.

而 javascript 什么都不做,就像它不存在一样。我尝试在标题、标题下方、正文、输入的 TD 中使用 JS...我也尝试在外部 .js 文件中使用它,没有,根本不起作用。我尝试将“更改”更改为“点击”,也没有奏效。我一整天都在尝试这样做,但我无法弄清楚出了什么问题。

So I tried to do much simpler code to check what's wrong and it seems like "change" or "onchange" - the second line of the JS doesn't work.

所以我试着做更简单的代码来检查哪里出了问题,看起来像“change”或“onchange”——JS的第二行不起作用。

I also tried to specify HTML5 doctype, doesn't do a thing.

我也尝试指定 HTML5 doctype,没有做任何事情。

My extra .html file to try even simpler code looks like this and of course doesn't work...

我额外的 .html 文件尝试更简单的代码看起来像这样,当然不起作用......

<!DOCTYPE html>
<html>
    <head>      
  </head> 
  <body>
    <table> 
        <tr>
            <td>Input field for click popup:</td>
            <td>
                <script type="text/javascript">
                var input = document.getElementById('input');
                input.addEventListener('click', function() {
                alert("Hello"); 
                });
                </script>
            <input type="text" id="input" />

            </td>
        </tr>
    <table>
    </body>  
</html>

Help me please, I don't really know what's this... I forgot to mention, I tried different browsers - Opera 12.15, latest FF and Chrome, didn't work in any case. But it works in fiddle... thanks for any help in advance

请帮助我,我真的不知道这是什么...我忘了提,我尝试了不同的浏览器 - Opera 12.15,最新的 FF 和 Chrome,在任何情况下都不起作用。但它适用于小提琴......提前感谢您的任何帮助

回答by Greg

Put the script right at the end of the body, right before the </body>end tag.

将脚本放在正文的</body>末尾,就在结束标记之前。

Scripts are loaded synchronously where they are placed, so any script put in beforethe element in question will not be aware of the element's existence.

脚本在放置它们的地方同步加载,因此任何放置在相关元素之前的脚本都不会知道该元素的存在。

Also, what Igor said.

还有,伊戈尔说的。

回答by Igor

You don't have a DOM element with id "myFile" in html that you included.

您在包含的 html 中没有 ID 为“myFile”的 DOM 元素。