javascript基础学习一

一、

1、= 赋值号

2、== 等于号

3、===完全等于号(如x=“2”,y=2,如果是x==y是true,若是x===y,则是flase)

二、var age=15 相当于 window.age=15

三、function sayage( ){

alert( "我"+age);

}

sayage( );------会出现弹窗显示我是15

相对于----------------------------------------------------------------

window.sayage=function(){

alert("我"+age);

}

sayage();------会出现弹窗显示我是15

四、var message=prompt("请输入您的星座","天蝎座");

console.log(message);----------将出现一个“请输入您的星座的”输入框,点击确认,则将天蝎座打印到控制台。

五、删除(绑定事件)注意:需要将JavaScript写在HTML语言前面

<body>
	<div id="box">
		<span>苹果XR</span>
		<input type="button" id="btn" value="删除">
	</div>
	
</body>
<script type="text/javascript">var btn=document.getElementById("btn");
			
			btn.onclick=function(){
				var result=window.confirm("您确认要删除吗");
				if (result) {
					document.getElementById("box").style.display="none";
				}

			}
		</script>
</html>

若要将JavaScript放到head里面,则需要将JavaScript的内容装载到window.onload = function(){ 内容 }里面,如下面代码:

<head>
<script type="text/javascript">var btn=document.getElementById("btn");
			
			window.onload = function(){
			btn.onclick=function(){
				var result=window.confirm("您确认要删除吗");
				if (result) {
					document.getElementById("box").style.display="none";
				}

			}}
		</script>
		</head>
		<body>
			<div id="box">
		<span>苹果XR</span>
		<input type="button" id="btn" value="删除">
	</div>
	
</body>
</html>

六、窗口关闭window.close

window.onload = function(){

				var btn = document.getElementById("btn");
				btn.onclick = function(){
					window.close();
				}
			}

	</script>
</head>
<body>
	
		<input type="button" id="btn" value="关闭窗口">
	</div>
	
</body>

</html>

七、打开新窗口window.open

window.onload = function(){

				var btn = document.getElementById("btn");
				btn.onclick = function(){
					window.open("oo.html"); //**打开oo.html的页面**
				}
			}

	</script>
</head>
<body>
	
		<input type="button" id="btn" value="打开新窗口">
	</div>
	
</body>

</html>

八、获取当前星期

var week=new Date().getDay();(获取当前日期
		switch(week){
			case 0:
				document.write("星期天");break;输出当前日期
			case 6:
				document.write("星期六");break;
			}

九、控制提示窗口弹出时间

1)第一种方法

setTimeout("alert('hello')",5000);  //5秒后弹出提示窗口

2)、第二中方法

var time=function(){
				alert("hello");
			}
			setTimeout(time,5000);

3)、第三种方法(匿名函数)

var timeout1=setTimeout(function(){
				alert("hello");
			},2000)

十、清除延时clearTimeout(id )

<script type="text/javascript">
		var timeout1=setTimeout(function(){
				alert("hello");
			},2000)
			clearTimeout(timeout1); //清除超时调用
	</script>

十一、屏幕间歇出现内容

1)第一种方法

var timeout1=setInterval(function(){  //var timeout1=可省略
				document.write("我爱你\n");
			},1000)

每隔一秒在屏幕出现 我爱你三个字

2)第二种方法

var timeout1=function() {   
				document.write("我爱你\n");
			}
			setInterval(timeout1,500);

十二、在多少时间后停止输出内容

var timeout1=setInterval(function(){
				document.write("我爱你\n");
			},1000)
			//在2秒后停止输出
			setTimeout(function(){
				clearTimeout(timeout1);
			},2000)

十三、在屏幕每隔一秒顺序输出1到10,当大于10的时候停止输出

1)第一种方法

var num=0,max=10,timer;
			timer=setInterval(function(){
				num++;
				if (num>10) {
					clearsetInterval(timer);
				}
				document.write(num+'\n');
			},1000);

2)第二种方法

var num=0,max=10,timer;
			function bb(){
				document.write(num+"\n");
				num++;
				if (num<max) {
					timer=setTimeout(bb,1000);
					}else{
						clearTimeout(timer);
					}
				}

			
			timer=setTimeout(bb,1000);

如图说明:在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weiwei266/article/details/83958381
今日推荐