很简单的javascript根本不起作用 [英] Very simple javascript doesn't work at all

查看:107
本文介绍了很简单的javascript根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.php(静态网站)文件中的javascript不起作用。我使用3个其他的脚本,他们做的工作,但不是这一个。



情况很简单



javascript是...

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

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

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

,PHP文件中的HTML代码(剥离)

 < form onsubmit =return anotherfunction(); action =sendForm.phpenctype =multipart / form-datamethod =post> 
< table>
< tr>
< td>附加图像:< / td>
< td> < input type =filename =priloha []accept =image / */>< / td>
< / tr>
< / table>
< / form>

JavaScript不做任何事情,它的行为就像它不存在。我试图在标题中使用JS,在标题下面,在正文中,在输入的TD中...我也尝试在外部.js文件中使用它,没有什么,根本不起作用。我尝试改变改变到点击,也没有工作。我试图一整天都这样做,我不知道是什么问题。



所以我试图做更简单的代码来检查是什么,看起来像改变或onchange - JS的第二行不起作用。



我还试图指定HTML5 doctype,不做一件事。 >

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

 <!DOCTYPE html> 
< html>
< head>
< / head>
< body>
< table>
< tr>
< td>点击弹出式输入字段:< / td>
< td>
< script type =text / javascript>
var input = document.getElementById('input');
input.addEventListener('click',function(){
alert(Hello);
});
< / script>
< input type =textid =input/>

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

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

解决方案

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



脚本在同一位置加载,所以任何

b $ b

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

The situation is simple

The javascript is...

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

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

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

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

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

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.

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

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

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

解决方案

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

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

Also, what Igor said.

这篇关于很简单的javascript根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆