<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <form name="form1"> 请输入年份:<input type="text" name="year" value=""> <button name="b1" onclick="cl()"> 是否是闰年 </button> </form> <script> var a; a=document.getElementByName('year'); function cl(a) { if((a%4==0)&&((a%100!=0)||(a%400==0)) { alert(a+"是闰年"); } else{ alert(a+"不是闰年"); } } </script> </body> </html>
时间: 2023-08-14 16:25:14 浏览: 48
这是一个 HTML 页面,包含一个表单和一些 JavaScript 代码。表单要求用户输入一个年份,并且当用户单击按钮时,JavaScript 代码会检查该年份是否是闰年,并弹出相应的结果。以下是代码的一些问题:
- `getElementByName` 是无效的方法,应该使用 `getElementsByName`。
- `getElementsByName` 返回一个节点列表,需要使用索引或循环来访问其中的元素。
- 在 `cl` 函数中,参数 `a` 是不必要的,因为它会被 `getElementsByName` 自动传入。
- 在 `cl` 函数中,需要使用 `a[0].value` 来获取用户输入的年份值。
下面是代码的修正版本:
```
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>判断闰年</title>
</head>
<body>
<form name="form1">
请输入年份:<input type="text" name="year" value="">
<button name="b1" onclick="cl()">是否是闰年</button>
</form>
<script>
function cl() {
var a = document.getElementsByName('year');
if ((a[0].value % 4 == 0) && ((a[0].value % 100 != 0) || (a[0].value % 400 == 0))) {
alert(a[0].value + "是闰年");
} else {
alert(a[0].value + "不是闰年");
}
}
</script>
</body>
</html>
```