老师,我这段代码麻烦检查一下哪儿错了
来源:2-13 编程练习
慕码人2001750
2021-05-25 11:18:58
相关代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="">
用户名:<input type="text" id='name' >
密码:<input type="text" id='pass' >
<button type="button" id="submit">登录</button>
</form>
<script>
var name = document.getElementById('name');
var pass = document.getElementById('pass');
var submit = document.getElementById('submit');
submit.onclick = function () {
if (name.value == '张三' && pass.value == '123456') {
window.location = 'https://www.imooc.com';
} else {
alert('信息不对');
}
}
</script>
</body>
</html>
相关截图:
1回答
好帮手慕星星
2021-05-25
同学你好,是name变量的原因,可以输出值看看
输出的是元素转化为字符串类型
这是因为window对象默认有name属性,值为空字符串
如果定义name变量并赋值,想等于给window.name赋值,元素就会变为字符串,使用的时候有冲突。
建议更换名称,例如:
祝学习愉快!
相似问题