使用object构造对象
<body>
<script type="text/javascript">
//自定义对象的创建
//使用object创建一个对象
var student =new Object
//给对象student添加属性stuId,stuName,className
student.stuId="11"
student.stuName="张三"
student.className="测试1"
//给对象添加一个函数
student.sayHello=function(){
console.log("大家好")
}
//对象名。函数名()实现函数调用
student.sayHello()
console.log(student)//console.log(student.id/name/class..)
//使用function创建对象
function teacher(tid,tname){
//行为
this.tid=tid
this.tname=tname
this.eat=function(){
//对象
console.log("吃饭")
}
}
var t1=new teacher("1","李四")
t1.eat()
console.log(t1.tid,t1.tname)
</script>
</body>
加减乘除的运算,例如此类弹框
<body>
第一个数:<input type="text" name="" id="one" /><br/>
第二个数:<input type="text" name="" id="two" /><br/>
//onclick单击事件
运算方式:<input type="button" name="" id="jiafa"value="+"οnclick="cal('+')" />
<input type="button" name="" id=""value="-"οnclick="cal('-')" />
<input type="button" name="" id=""value="*"οnclick="cal('*')" />
<input type="button" name="" id=""value="/"οnclick="cal('/')" /><br/>
运算结果:<input type="text" name="result" id="result"value="" />
<script type="text/javascript">
function cal(a){
//实现两个数相加
//获取文本框的值
var one=document.getElementById("one").value
var two=document.getElementById("two").value
//获取后计算,注意要转换为数字类型
var result=0
if(a=="+"){
result= parseFloat(one)+parseFloat(two)
}else if(a=="-"){
result= parseFloat(one)-parseFloat(two)
}else if(a=="*"){
result= parseFloat(one)*parseFloat(two)
}else if(a=="/"){
result= parseFloat(one)/parseFloat(two)
}
document.getElementById("result").value=result
}
字符串对象常用方法
举例<body>
<script type="text/javascript">
//字符串对象
var str="hello word"
//获取字符串长度
console.log(str.length)
//判断是否包含某字符 ""和''性质一样
console.log(str.indexOf("l"))
console.log(str.indexOf('b'))
//截取字符 截取字符中的一段
console.log(str.substring(0,5))
//获取字符中的几个字符
console.log(str.substr(2,5))
</script>
判断邮箱格式是否正确