JavaScript学习
1.typeof, null, 和 undefined
我们可以使用 typeof 去检测变量是什么数据类型。
使用语句typeof "dog"
,使整个程序可以返回它的数据类型string ,但数组是 一种特殊的数据类型,使用 typeof会返回 object而不是其他的。
null也是一个特殊类型,它是一个对空对象的引用,使用 typeof也会返回 object。
undefined 表示的是一个没有设置值的变量, 一个没有值的变量会自动赋值为undefined。如语句var a;
a的值自动赋为为 undefined, 类型也是undefined。
2.JavaScript 表单
例:判断密码是否正确
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>请输入您的密码</p>
<input id="password">
<button type="button" onclick="myFunction()">确认密码</button>
<p id="demo"></p>
<script>
function myFunction() {
var x, text;
x = document.getElementById("password").value;
if (x==123456) {
alert("您的密码正确")
} else{
alert("您的密码错误,请重新确认。")
}
}
</script>
</body>
</html>
当输入的数据为123456时,网页会自动弹出"您的密码正确"的消息,否则,网页将弹出"您的密码错误,请重新确认。"的消息。