我试图为我的网站生成 Javascript 警报功能,以便它们会弹出,但是由于某些不明原因 - 它们不起作用。我已经使用 W3C 验证器测试了我的代码,这导致了 3 个警告,其中之一说我错过了正文标签,但我在代码中没有看到任何错误。请帮忙?
<!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>
最佳答案
不能仅使用数字作为函数名称。更改如下:
function myFunction() {
alert("10/63");
}
function a2() {
alert("13/10");
}
function a3() {
alert("37/30");
}
function a4() {
alert("4/21");
}
HTML 应该像这样改变:
<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>
关于Javascript 警报功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25217421/