【JS学习笔记】第一个JS效果——鼠标提示框
分析效果实现原理——鼠标提示框
样式:div的display
事件:onmouseover,onmouseout
编写JS的流程
布局:HTML+CSS
属性:确定需要修改哪些属性
事件:确定用户做哪些操作(产品设计),确定要在什么样的事件夏进行修改,是点击还是移入移出。
编写JS:在事件中,用JS来修改页面元素的样式
鼠标提示框的简单练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div1{
width:200px;
height:100px;
background:#ccc;
border:1px solid #999;
display:none;/*该区域一开始是隐藏的*/
}
</style>
</head>
<input type="checkbox" onmouseover="document.getElementById('div1').style.display='block'" onmouseout="document.getElementById('div1').style.display='none'"/>
<div id="div1">
为了您的安全
</div>
<body>
</body>
</html>
然后引入函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div1{
width:200px;
height:200px;
background:red;
}
</style>
<script>
function toGreen()
{
document.getElementById('div1').style.width='300px';
document.getElementById('div1').style.height='300px';
document.getElementById('div1').style.background='green';
}
function toRed()
{
document.getElementById('div1').style.width='200px';
document.getElementById('div1').style.height='200px';
document.getElementById('div1').style.background='red';
}
</script>
</head>
<body>
<div id="div1" onmouseover="toGreen()"
onmouseout="toRed()">
</div>
</body>
</html>
其实这段代码中函数里的好多代码是重复的,看上去太烦太多,可以利用变量来简化。
浙公网安备 33010602011771号