警报不工作的按钮点击

Alert not working on button click

本文关键字:按钮 工作      更新时间:2023-09-26

我很好奇为什么当我点击按钮时它不警报5。有人知道为什么吗?http://jsfiddle.net/bm8dd/

<center>
   <input id='1' type='text' onfocus='1();'>
   <input id='2' type='text' onfocus='2();'>
   <br>
   <br>
   <button type='button' onclick='3()'>Button</button>
</center>
<script>
    var x = 5;
    function 3() {
        alert(x);
    }
</script>

因为函数名不能以数字开头。

同样,id (不是现在)也是一样的,但是一般的做法是id也不要以数字开头。

考虑适当地命名你的函数,如field1()field2(),这样当其他人读你的代码时就有意义了,这叫做命名约定

3不是有效的函数名。函数必须以字母(A-Za-z)或$字符开头。

另外,请避免使用<center>,因为它不再是有效的HTML。

遵循命名约定,
函数名不能以数字开头

Try as one, two, three

    <input id='1' type='text' onfocus='one();'>
    <input id='2' type='text' onfocus='two();'>
    <br>
    <br>
     <button type='button' onclick='three()'>Button</button>

在脚本标签之间

     var x = 5;
     function three() {
         alert(x);
     }

你不能为函数命名数字,试试这个,

<input type="button" value="test" />

    $('input[type=button]').click( function() {
     alert("test");   
    });

有时在执行警报功能时jsfiddle可能是一个麻烦。

你必须确保分开你的html cssjs尽可能多,留下script tags

下面是一个工作示例,

点击这里