欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1.0.2 js中数组,对象,属性,表单验证

程序员文章站 2022-04-12 22:52:26
...
<!DOCTYPE html>
<html>
<head>
<script>
//数组的用法
function showArray(){
/*
var array=new Array();
array[0]="张三";
array[1]="李思";
*/
//var array=new Array('张三','李思');
var array=['张三','李思','王五'];

var html="";
for(i=0;i<array.length;i++){
html=html+array[i]+"-";
}
document.getElementById("demo1").innerHTML=html;
}

//显示对象
function showObject(){
//对象由花括号分隔。在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义。属性由逗号分隔:
//第一种x写法
var person={firstname:"Bill", lastname:"Gates", id:5566};
//第二种写法
var person2={
firstname:"李",
lastname :"思",
id :5566
};
//第三种写法
var person=new Object();
person.firstname="王";
person.lastname="X";


var html="";
html=person.firstname + person.lastname;
html=html+"----"+person2["firstname"]+person2["lastname"] +" ====";
document.getElementById("demo1").innerHTML=html;

}


//显示对象属性
function showObjectAttr(){
var person2={
firstname:"李",
lastname :"思",
id :5566
};
var attrs=null;
var x;
for(x in person2){
attrs = attrs + person2[x]+ ',';
}
document.getElementById("demo1").innerHTML=attrs;
}

//try...catch用法
function tryCatch(){
try{
//故意出错,进程捕捉
atter("try..catch....");

}catch(err){
alert("出错啦");
}

}


//表单验证
function validate_form(thisForm){
with(thisForm){
if(validate_email(email,'Not a valid email address')==false){
email.focus();
return false;
}
}
}

//email验证
function validate_email(field,errText){
with(field){
var val=field.value;
var pos1=val.indexOf('@');
var pos2=val.indexOf('.');
if(pos1 < 1 || pos2 < pos1){
alert(errText);
return false;
}else{
return true;
}

}
}


</script>
</head>
<body>
<div id="demo1"></div>
<button onclick="showArray()">显示数组</button>
<button onclick="showObject()">显示对象</button>
<button onclick="showObjectAttr()">遍历对象属性</button>
<button onclick="tryCatch()">TRY-CATCH</button>
<br />
<form action="submitpage.html" onsubmit="return validate_form(this)" method="post">
Email:<input type="text" name="email" size="30" />
<input type="submit" value="Submit">

</form>

<body>
</html>




                                                           @dianxinxinxiyuan.xiuyanxilu.pudongqu.shanghai