JavaScript笔记01----鼠标提示

HTML+CSS生成静态网页
JS可以在静态网页上添加交互和功能
部分事件可以理解为用户的操作

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>无标题文档</title>
		<style>
			#div1 {width: 200px;height: 100px;background: #55aaff;border: 1px solid #999;}
		</style>
	</head>
	<body>
		<input type="checkbox" />
		<div id="div1">
			为了您的信息安全,请...
		</div>
	</body>
</html>

效果:
1
设置display:none;可以把文字隐藏

目标:鼠标移入到input上的时候,让div1显示,鼠标移出input的时候,让div1隐藏

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>无标题文档</title>
		<style>
			#div1 {width: 200px;height: 100px;background: #55aaff;border: 1px solid #999;display: none;}
		</style>
	</head>
	<body>
		<input type="checkbox" onmouseover="div1.style.display='block';" onmouseout="div1.style.display='none';"/>
		<div id="div1">
			为了您的信息安全,请...
		</div>
	</body>
</html>

2

在有些浏览器中,可能要把下面的代码作更改才能达到效果:

<input type="checkbox" onmouseover="div1.style.display='block';" onmouseout="div1.style.display='none';"/>

换成:

<input type="checkbox" onmouseover="document.getElementById('div1').style.display='block';" onmouseout="document.getElementById('div1').style.display='none';"/>

特效原理概括:响应用户操作,对页面元素(标签)进行某种修改

可以借用变量和函数实现代码重用

继续更改

<input type="checkbox" onmouseover="document.getElementById('div1').style.display='block';" onmouseout="document.getElementById('div1').style.display='none';"/>

更改后:(函数在script标签里面)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>无标题文档</title>
		<style>
			#div1 {
			width: 200px;
			height: 100px;
			background: #55aaff;
			border: 1px solid #999;
			display: none;}
		</style>
		<script>
			function showtext()
			{document.getElementById('div1').style.display='block';}
			function hidetext()
			{document.getElementById('div1').style.display='none';}
		</script>
	</head>
	<body>
		<input type="checkbox"
		 onmouseover="showtext()"
		 onmouseout="hidetext()"/>
		<div id="div1">
			为了您的信息安全,请...
		</div>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值