<body>
第一个数:<input type="text" name="" id="one" value="" /><br />
第二个数:<input type="text" name="" id="two" value="" /><br />
运算方式:<input type="button" name="" id="jiafa" value="+" onclick="cal_1('+')"/>
<input type="button" name="" id="" value="-" onclick="cal_1('-')"/>
<input type="button" name="" id="" value="*" onclick="cal_1('*')"/>
<input type="button" name="" id="" value="/" onclick="cal_1('/')"/><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)
}else if(y=="*"){
result=parseFloat(one)*parseFloat(two)
}else(y=="/"){
result=parseFloat(one)/parseFloat(two)
}
document.getElementById("result").value=result
}
</script>
</body>
属性、方法和事件
属性:属性是指对象包含的值,使用‘对象名.属性名’的方式进行操作,如 document。myfrom.first。value
方法:在代码里,使用‘对象名.方法名()’来调用该对象的方法。
alter(“)=windowalter(“)
事件:响应用户操作、完成交互,如OnClick OnKeyDown
一般可以分为鼠标事件、键盘事件及其他事件
使用object创建一个对象
<body>
<script type="text/javascript">
var student=new Object()
// console.log(student)
//给对象student添加属性试图ID ,stuName,className
student.stuID="1001"
student.stuName="张三"
student.className="移动2班"
//给对象添加了一个函数sayhello()
student.sayhello=function(){
console.log("大家好")
}
//console.log(student)
//对象名.函数名()实现函数的调用
student.sayhello()
console.log(student.stuID)
</script>
</body>
使用function创建一个对象
<body>
<script type="text/javascript">
function teacher(tid,tname){
//this表示当前对象
this.tid=tid//教师编号
this.tname=tname
this.eat=function(){
//行为
console.log("吃饭")
}
}
var t1=new teacher("1128","易烊千玺")
//使用t1来访问属性和函数
//调用函数
t1.eat()
console.log(t1.tid,t1.tname)
</script>
</body>
字符串对象
用于存储一系列字符
使用单引号或双引号包含
var name="河南厚溥";
var http=sx.hnhpit.com';
可以使用索引访问字符串中任何的字符
var char=http[5];
兼容性差,只能兼容高版本浏览器,不能兼容IE6-8
可以在字符串中使用引号
var full =“河南厚溥'sx.hnhpit.com'";
full="河南厚溥\"sx.hnhpit.com\""
<body>
<script type="text/javascript">
//字符串对象
var str="hello world!"
console.log(str.length)//length用来获取字符串的长度
console.log(str.charAt(2))
// function test(){
// console.log("aa")
// }
// console.log(test())
console.log(str.indexOf("l"))
console.log(str.indexOf("b"))
//判断是否包含某个字符
console.log(str.substring(0,5))//[0,5)
//substr():第一个参数指的是索引 第二个参数指的是截取的字符串的长度
console.log(str.substr(1,2))
</script>
</body>