对象定义
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
去除属性值
person.lastName;
例子:与innerhtml组合点按查询属性
<!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>
<script>
var person={name:"危笑",id:"20185358"}
function show(){
document.getElementById("demo").innerHTML="姓名:"+person.name+"学号:"+person.id;
// document.getElementById("demo").innerHTML=person.id
}
</script>
</head>
<body>
<div id="demo"></div>
<button type="button" onclick="show()">显示信息</button>
</body>
</html>
函数传参
注意给的参数要单引号
<!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>
<script>
function show(name,id){
alert(name,id);
}
</script>
</head>
<body>
<button onclick="show('危笑','20185358')">点击</button>
</body>
</html>
js验证表单
也就是不给服务器,onsubmit()拦截,如果填写有误直接不给提交
<!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>
<script>
function check(){
var x=document.forms["demo"]["id"].value;
if (x==null||x==""){
alert("未填写信息");
return false;
}
}
</script>
</head>
<body>
<form name="demo" action="" method="post" onsubmit="check()">
<input type="text" name="id">
<input type="submit" value="提交给js验证是否为空">
</form>
</body>
</html>```