admin管理员组文章数量:1424415
I have attempted to produce Javascript Alert functions for my website so that they would pop up,however for some unidentified reason-they do not work.I have tested my code with W3C Validator,which resulted in 3 warnings,one of which said I had missed a body tag,yet I do not see any error within my code.Please assist?
<!DOCTYPE html>
<html>
<body>
<p>Click the button.</p>
<button onclick="myFunction()">Example 1 Answer</button>
<br>
<button onclick="2">Example 2 Answer</button><br>
<button onclick="3">Example 3 Answer</button><br>
<button onclick="4">Example 4 Answer</button>
<script>
function myFunction() {
alert("10/63");
}
function 2() {
alert("13/10");
}
function 3() {
alert("37/30");
}function 4() {
alert("4/21");
}
</script>
</body>
</html>
I have attempted to produce Javascript Alert functions for my website so that they would pop up,however for some unidentified reason-they do not work.I have tested my code with W3C Validator,which resulted in 3 warnings,one of which said I had missed a body tag,yet I do not see any error within my code.Please assist?
<!DOCTYPE html>
<html>
<body>
<p>Click the button.</p>
<button onclick="myFunction()">Example 1 Answer</button>
<br>
<button onclick="2">Example 2 Answer</button><br>
<button onclick="3">Example 3 Answer</button><br>
<button onclick="4">Example 4 Answer</button>
<script>
function myFunction() {
alert("10/63");
}
function 2() {
alert("13/10");
}
function 3() {
alert("37/30");
}function 4() {
alert("4/21");
}
</script>
</body>
</html>
Share
Improve this question
edited May 31, 2016 at 6:20
Serenity
36.8k21 gold badges123 silver badges116 bronze badges
asked Aug 9, 2014 at 10:05
DarkRunnerDarkRunner
4474 silver badges20 bronze badges
3
- upvote for this little boy with big dreams – doniyor Commented Aug 9, 2014 at 10:08
- It's appreciated-thanks. – DarkRunner Commented Aug 9, 2014 at 10:10
- Hmmm,...still not working-as always,only the first fraction,10/63 is working. – DarkRunner Commented Aug 9, 2014 at 10:17
5 Answers
Reset to default 5You cannot have just a number as a function name. Change it like following:
function myFunction() {
alert("10/63");
}
function a2() {
alert("13/10");
}
function a3() {
alert("37/30");
}
function a4() {
alert("4/21");
}
And the HTML should change like this:
<p>Click the button.</p>
<button onclick="myFunction()">Example 1 Answer</button>
<br>
<button onclick="a2()">Example 2 Answer</button>
<br>
<button onclick="a3()">Example 3 Answer</button>
<br>
<button onclick="a4()">Example 4 Answer</button>
Demo
Function names can't begin with digits. If you copy paste your following function in the console you will get unexpected number
reference.
function 2() { alert("4/10"); }
Use for old browsers:
<script language="text/javascript">
Function names can not start with numbers, fix that also.
Edit:
For HTML4
<script type="text/javascript">
For HTML5
<script>
http://jsfiddle/c1pcc4xv/
<form action="">
<input type="button" id="myButton" value="Add"/>
</form>
//script
var myBtn = document.getElementById('myButton');
myBtn.addEventListener('click', function(event) {
alert("yo");
});
make your code simple, and avoid function names starting with number....Hope it helps
Here is a JS Fiddle
Yes, you can not use just numbers as a function names. And also, wrap it in the head tag, define your function before calling it in your html.
function myFunction() {
alert("10/63");
}
function myFunction2() {
alert("13/10");
}
function myFunction3() {
alert("37/30");
}
function myFunction4() {
alert("4/21");
}
本文标签: htmlJavascript Alert Function not workingStack Overflow
版权声明:本文标题:html - Javascript Alert Function not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745379072a2656074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论