一、属性、方法和事件
1.属性
属性是指对象包含的值,使用" 对象名.属性名 "的方式进行操作,如document.forst.value
源代码:
第一个数:<input type="text" id="one" value=""/><br />
第二个数:<input type="text" id="two" /><br />
<script type="text/javascript">
var one=document.getElementById("one").value//获取文本框内容
var two=document.getElementById("two").value
试图:
2.事件
影响用户操作、完成交互、如OnClick、OnKeyDown
一般可以分为鼠标事件、键盘事件及其他事件、
源代码:
第一个数:<input type="text" id="one" value=""/>+
第二个数:<input type="text" id="two" />=
<input type="button" name="" id="" value="添加" onclick="cal('+')"/>
<input type="texe" name="result" id="result" value="" />
<script type="text/javascript">
function cal(){ //获取单击事件
var one=document.getElementById("one").value//获取文本框内容
var two=document.getElementById("two").value
var result=Number(one)+Number(two)
document.getElementById("result").value=result
试图:
3.自定义对象
--字符串对象
用于存储一系列字符,使用单引号或双引号包含
--数学对象
用于获取各种数学常量及数学函数
--日期对象
用于获取或操作各种时间
开发人员根据自己的需要而定义新对象
1.1自定义对象,使用Object
var student=new Object()
给对象设置一些属性
var student=new Object()
student.stuid="1001"
student.stuname="小王"
student.classname="移动3班"
给对象设置函数
student.sayHello=function(){
console.log("大家好")
}
student.sayHello()
console.log(student.stuid)
function 函数名(参数列表)(函数体)
赋值式声明函数
var test=function(){
console.log("aa")
}
test()
1.2使用构造函数
构造函数中的this指的是当前对象
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)
1.3其他方式
var stu={
stuid:"1002",
stuname:"小王",
study:function(){
console.log("学习")
}
}
stu.study()
console.log(stu.stuid)
用于储存一系列字符
使用单引号和双引号包含
var name = "优快云"
var name = '优快云'
可以使用索引访问字符串中任何的字符
var char = http[5];