<!-- text文本框 button单选按钮 -->
第一个数:<input type="text" name="" id="one" value="" /><br />
第二个数:<input type="text" name="" id="two" value="" /><br />
运算方式:
<input type="button" name="ys" id="ys" value="+" onclick="cal('+')"/>
<input type="button" name="ys" id="ys" value="-" onclick="cal('-')"/>
<input type="button" name="ys" id="ys" value="*" onclick="cal('*')"/>
<input type="button" name="ys" id="ys" value="/" onclick="cal('/')"/><br />
<!-- onclick="cal()" 是单击按钮是点击事件-->
运算结果:
<input type="text" name="" id="result" value="" /><br />
<script type="text/javascript">
function cal(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 if (y=="/") {
result=parseFloat(one)/parseFloat(two)
}
// 也可以使用Switch...case来实现
document.getElementById("result").value=result
}
</script>
属性:是指对象包含的值,使用‘对象名.属性名’的方式进行操作,
方法:在代码里,使用‘对象名.方法名( )’来调用该对象的方法
事件:相应用户操作,完成交互,一般可以分为鼠标事件,键盘事件及其他事件
自定义对象:开发人员根据自己的需要而定义的新对象
JavaScript内置对象:JavaScript将一些常用功能预先定义成对象,用户可以直接使用。如字符串对象,数学对象,日期对象,数组对象,History对象等
自定义对象的创建,可以使用object创建,使用function创建
// 字符串对象
var str="Hello World!"
console.log(str.length) //length用来获取字符串的长度
console.log(str.charAt(2)) //返回参数指定索引处的字符
// 返回参数在字符串中首次出现的位置
console.log(str.indexOf("l"))
console.log(str.indexOf("b"))
// 判断是否包含某个字符
console.log(str.substring(0,5)) // 0 1 2 3 4 不包含第五个
console.log(str.substr(1,2))
// 从第一个索引值开始 返回两个值
// substr():第一个参数指的是索引 第二个参数指的是截取的字符串的长度