JS練習題 ( 下拉式功能表;好友選中輸入)

來源:互聯網
上載者:User

標籤:

題一、左側菜單下拉

做題思路:先做菜單和子功能表,把子功能表預設隱藏。再用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練習題 ( 下拉式功能表;好友選中輸入)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.