js的dom操作,onmouseover事件,onmouseleave事件,定时器

7 篇文章 0 订阅
7 篇文章 0 订阅

样式

onmouseover鼠标放上改变触发事件
onmouseleave鼠标离开改变触发事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type	= "text/css">
			#box{
				width: 200px;
				height: 200px;
				background-color: beige;
			}
		</style>
	</head>
	
	<body>
		<div id= "box" 
			onmouseover="changeColor()"
			onmouseleave ="changeColor2()">
			
		</div>
	</body>
</html>
<script type="text/javascript">
	//onmouseover鼠标放上改变触发事件
	//onmouseleave鼠标离开改变触发事件
	function changeColor(){
		var div = document.querySelector("#box");
		//修改样式css,方式一
		div.style.backgroundColor = "brown";
		div.innerHTML = "我来了...";
	}
	function changeColor2(){
		var div = document.querySelector("#box");
		//修改样式css,方式一
		div.style.backgroundColor = "green";
		div.innerHTML = "我走了...";
	}
</script>

效果展示

在这里插入图片描述

隐藏 修改css样式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type	= "text/css">
			#box{
				width: 200px;
				height: 200px;
				background-color: beige;
			}
		</style>
	</head>
	<body>
		<button onclick="change()">隐藏</button>
		<div id= "box" >
			
		</div>
	</body>
</html>
<script type="text/javascript">
	//点击隐藏按钮让当前div隐藏
	var flag = 0;
	function change(){
		var div = document.querySelector("#box");
		var button1 = document.querySelector("button");
		if(flag==0){
			//修改样式css,方式一
			div.style.display= "none";
			button1.innerHTML = "显示";
			flag = 1;
		}else{
			//修改样式css,方式一
			div.style.display= "block";
			button1.innerHTML = "隐藏";
			flag = 0;
		}
		
	}
	
</script>

效果展示

在这里插入图片描述

className同时修改多个样式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			.c1{
				width: 200px;
				height: 200px;
				background-color: burlywood;
				border: 5px solid yellow;
			}
			.c2{
				width: 200px;
				height: 200px;
				background-color: green;
				border: 5px solid rosybrown;
			}
		</style>
		
	</head>
	<body>
		<button onclick="change1()">切换样式</button>
		<div id="d1" class="c1">
			
		</div>
	</body>
</html>
<script type="text/javascript">
	var flag = "c1";//首先定义变量样式为c1
	function change1(){
		var div = document.querySelector("#d1");
		if (flag == "c1") {
			flag = "c2";
		} else{
			flag = "c1";
		}
		//使用className同时修改多个样式(将样式在css里面提前定义好).
		div.className = flag;
	}
</script>

延迟执行定时器

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		
		<button onclick="start2()">延迟执行</button>
		<button onclick="stop1()">停止延迟</button>
	</body>
</html>
<script type="text/javascript">
	function start1(){
		alert("延迟2秒,只执行一次.")
	}
	var time1= null;
	function start2(){
		time1 = setTimeout("start1()",4000);
	}
	function stop1(){
		clearTimeout(time1);
	}
	
</script>

立即执行定时器

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<div id="box">
			
		</div>
		<button onclick="stop1()">停止计时</button>
	</body>
</html>
<script type="text/javascript">
	function getCurrentTime(){
		var div=document.querySelector("#box");
		var date = new Date();
		var d = date.getFullYear()+"/"+
		(date.getMonth()+1)+"/"+
		date.getDate()
		+" " +
		date.getHours()+":"+
		date.getMinutes()+":"+
		date.getSeconds();
		console.log(d);
		div.innerHTML = "系统当前时间" + d;
	}
	
	//定时调用
	var start = setInterval("getCurrentTime()",1000);
	function stop1(){
		clearInterval(start);
	}
	
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值