admin管理员组文章数量:1025286
I am curious why it doesnt alert 5 when i click the button. anyone know why? /
<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>
I am curious why it doesnt alert 5 when i click the button. anyone know why? http://jsfiddle/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>
Share
Improve this question
edited Jul 30, 2014 at 5:48
Alkis Kalogeris
17.8k15 gold badges62 silver badges118 bronze badges
asked Jul 30, 2014 at 5:41
user3716244user3716244
71 bronze badge
3
- 2 Tips: 1. Learn to use jsFiddle, put JS code in its own panel, click the JSHint button to find about errors. 2. Check your browser console. – elclanrs Commented Jul 30, 2014 at 5:43
-
Use your console:
Uncaught SyntaxError: Unexpected number
. – Marty Commented Jul 30, 2014 at 5:43 -
1
If you absolutely must have functions named this way, you could do something like
window["1"] = function(){ ... }
, called viawindow["1"]()
. – Marty Commented Jul 30, 2014 at 5:46
4 Answers
Reset to default 7Because you cannot start a functions name with a number.
Also, its same with the id (not now) but its a general practice of not to start the ids with the numbers too.
Consider naming your functions appropriately like field1()
or field2()
so it makes sense when someone else reads your code, that's called Naming Convention.
3
is not a valid function name. Functions must begin with an alphabetical character (A-Za-z
) or $
character.
Also, please avoid <center>
as it is no-longer valid HTML.
Follow the naming conventions,
You cannot start a function name with numbers
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>
Between script tags
var x = 5;
function three() {
alert(x);
}
You can not name functions numbers, try this,
<input type="button" value="test" />
$('input[type=button]').click( function() {
alert("test");
});
Sometimes jsfiddle can be a pain when doing alert functions.
You have to make sure to separate your html
css
and js
as much as you can and leave out script tags
Here is a working example,
Click Here
I am curious why it doesnt alert 5 when i click the button. anyone know why? /
<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>
I am curious why it doesnt alert 5 when i click the button. anyone know why? http://jsfiddle/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>
Share
Improve this question
edited Jul 30, 2014 at 5:48
Alkis Kalogeris
17.8k15 gold badges62 silver badges118 bronze badges
asked Jul 30, 2014 at 5:41
user3716244user3716244
71 bronze badge
3
- 2 Tips: 1. Learn to use jsFiddle, put JS code in its own panel, click the JSHint button to find about errors. 2. Check your browser console. – elclanrs Commented Jul 30, 2014 at 5:43
-
Use your console:
Uncaught SyntaxError: Unexpected number
. – Marty Commented Jul 30, 2014 at 5:43 -
1
If you absolutely must have functions named this way, you could do something like
window["1"] = function(){ ... }
, called viawindow["1"]()
. – Marty Commented Jul 30, 2014 at 5:46
4 Answers
Reset to default 7Because you cannot start a functions name with a number.
Also, its same with the id (not now) but its a general practice of not to start the ids with the numbers too.
Consider naming your functions appropriately like field1()
or field2()
so it makes sense when someone else reads your code, that's called Naming Convention.
3
is not a valid function name. Functions must begin with an alphabetical character (A-Za-z
) or $
character.
Also, please avoid <center>
as it is no-longer valid HTML.
Follow the naming conventions,
You cannot start a function name with numbers
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>
Between script tags
var x = 5;
function three() {
alert(x);
}
You can not name functions numbers, try this,
<input type="button" value="test" />
$('input[type=button]').click( function() {
alert("test");
});
Sometimes jsfiddle can be a pain when doing alert functions.
You have to make sure to separate your html
css
and js
as much as you can and leave out script tags
Here is a working example,
Click Here
本文标签: javascriptAlert not working on button clickStack Overflow
版权声明:本文标题:javascript - Alert not working on button click - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614174a2159184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论