JavaScript

<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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值