一,内置函数
1.parselnt()函数2.parseFloat()函数3.isNaN()函数4.eval()函数
第一个数: <input type="text" id="one" /><br />
第二个数: <input type="text" id="two" /><br />
运算方式:<input type="button" name="" id="jiafa" value="+" onclick="cal_1()"/>
<input type="button"name="" id="" value="-" onclick="cal_2()"/>
<input type="button"name="" id="" value="*" onclick="cal_3()"/>
<input type="button"name="" id="" value="*" onclick="cal_4()"/><br />
运算结果:<input type="text" name="result" id="result" value=""/>
<script type="text /javascript">
function cal_1(y){
//获取文本框的值
var one=document.getElementById("one").Value
var two=document.getElementById("two").Value
var result=0
if(y=="+"){
result=parseFloat(one) +parseFloat(two)
}else if(y=="-"){
result=parseFloat(one)-parseFloat(two)
}
document.getElementById(""result).value=result
}
二,JavaScript中的对象
属性
属性是指对象包含的值,使用' 对象名.属性名' 的方式进行操作,如
document.myfrom.first.value
方法
在代码里,使用'对象名.方法名() ' 来调用该对象的方法
alter('')=Window.alter('')
事件
响应用户操作,完成交互,如OnClick, OnKeyDown
一般可以分为鼠标事件,键盘事件及其他事件
自定义对象
开发人员根据自己的需要而定义的新对象
JavaScript内置对象
JavaScript将一些常用功能预定义成对象,用户可以直接使用,这就是内置对象。
如字符串对象,数学对象,日期对象,数组对象。 正则表达式对象等
浏览器内置对象
浏览器对象是浏览器根据系统当前的配置和所装载的页面为JavaScript提供的一系列可供使用的对象。
如Window对象,Document对象,History对象等
<body>
<script type="text/javascript">
//自定义对象的创建
//使用Object创建一个对象
var student=new Object()
//console.log(student)
//给对象student添加属性stuID、stuName、className
student.stuID="1001"
student.stuName="张三"
student.className="移动2102班"
//给对象添加一个函数sayHello()
student.sayHello=function(){
console.log("大家好")
}
// console.log(student)
//对象名.函数名()实现函数的调用
student.sayHello()
console.log(student.stuID)
console.log(student.stuName)
console.log(student.className)
//使用funtcion创建一个函数
function teacher(tid,tname){
//this表示当前对象
this.tid=tid//教师编号
this.tname=tname//教师姓名
this.eat=function(){//行为
console.log("吃饭")
}
}
var t1=new teacher("1","李晓敏")
//使用t1来访问属性和函数
//调用函数
t1.eat()
console.log(t1.tid,t1.tname)
</script>
</body>