標籤:
題一、左側菜單下拉
做題思路:先做菜單和子功能表,把子功能表預設隱藏。再用JS調樣式。
<style type="text/css">
*{ margin:0px auto; padding:0px}
.text1{ width:180px; height:39px; border-bottom:1px solid white; text-align:center; line-height:40px; vertical-align:middle; color:white}
.text1:hover{ cursor:pointer}
.text2{ width:160px; height:40px; float:left; font-family:微軟雅黑}
.text2:hover{ color:#F00}
.text3{ width:20px; height:40px; float:left; color:white}
.text4{ width:178px; height:40px; text-align:center; line-height:40px; vertical-align:middle; border-left:1px solid #999; border-right:1px solid #999}
.zi{ display:none}//所有的子功能表隱藏
#z33{ border-bottom:1px solid #999}
</style>
</head>
<body>
<div style="width:500px; height:500px">
<div id="shouye" class="text1">
<div class="text2" onclick="Show()">首頁</div>
<div class="text3">></div>
</div>
<div id="jiaowu" class="text1"; onclick="Show(‘z1‘)">
<div class="text2">教務資訊</div>
<div class="text3">></div>
</div>
<div class="zi" id="z1">
<div class="text4">查看通知</div>
<div class="text4">發送通知</div>
</div>
<div id="xiazai" class="text1" ; onclick="Show(‘z2‘)">
<div class="text2">下載專區</div>
<div class="text3">></div>
</div>
<div class="zi" id="z2">
<div class="text4">簡曆檔案下載</div>
<div class="text4">教師評測下載</div>
<div class="text4">其它下載</div>
</div>
<div id="xueyuan" class="text1" ; onclick="Show(‘z3‘)">
<div class="text2">學員資訊管理</div>
<div class="text3">></div>
</div>
<div class="zi" id="z3">
<div class="text4">項目</div>
<div class="text4">考試</div>
<div class="text4" id="z33">作業</div>
</div>
</div>
</body>
<script type="text/javascript">
function Show(id)//用變數id(形參)接收根據接收的變數找到要操作的項。
{
var z = document.getElementById(id);//點擊主菜單,子功能表顯示
if(z.style.display=="block")//如果原先子功能表是顯示的
{
z.style.display="none";//讓原先顯示的隱藏
}
else//如果原先是隱藏的
{
z.style.display="block";//讓原先隱藏的顯示
}
}
</script>
題二、好友名單選中
*{ margin:0px auto; padding:0px}
.text{ width:150px; height:35px; border:2px solid white; text-align:center; line-height:30px; vertical-align:middle; color:white}
.text:hover{ cursor:pointer;}<!--用hover改變顏色後面滑鼠放上後顏色會無效,下面用的onmouseover變色滑鼠放上後的背景色。-->
</style>
</head>
<body>
<div style="width:500px; height:500px; margin-top:30px">
<div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">叫一聲</div>
<div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">佛祖</div>
<div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">回頭</div>
<div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">無岸</div>
</div>
</body>
<script type="text/javascript">
function Show(a)//用一個形參接收變數
{
var attr = document.getElementsByClassName("text");//找到所有的好友
for(i=0;i<attr.length;i++)
{
attr[i].style.backgroundColor="#63C";//先背景色設定為是預設的顏色,防止點擊一個之前點擊更換的背景色不變回預設的顏色。
attr[i].setAttribute("xz","0");//清別的選項的背景色,設定為0.
}
a.setAttribute("xz","1");//代表選中,設定為1.
a.style.backgroundColor="#F63";//點擊換背景色
}
function Bian(a)//滑鼠放上以後背景色改變,但是原先滑鼠選定的背景顏色會改變成預設顏色。
{
var attr = document.getElementsByClassName("text");
for(i=0;i<attr.length;i++)
{
if(attr[i].getAttribute("xz")=="0")//要在前面加上xz的屬性,預設等於0。
{
attr[i].style.backgroundColor="#63C";
}
}
a.style.backgroundColor="#F63";
}
function Hui(a)//滑鼠離開後顏色返回預設顏色
{
var attr = document.getElementsByClassName("text");
for(i=0;i<attr.length;i++)
{
if(attr[i].getAttribute("xz")=="0")
attr[i].style.backgroundColor="#63C";
}
}
</script>
JS練習題 ( 下拉式功能表;好友選中輸入)