基于Javascript實現(xiàn)二級聯(lián)動菜單效果

字號:


    本文實例為大家分享了Javascript實現(xiàn)二級聯(lián)動菜單效果的對應代碼,具體內(nèi)容如下
    具體實現(xiàn)步驟如下:
    1.所用js代碼如下:
    <script type="text/javascript">
    var arr_province=["請選擇省份/城市","北京市","上海市","天津市","河南省","山東省","河北省"];
    var arr_city=[
            ["請選擇城市/地區(qū)"],
            ["中關村","海淀區(qū)","朝陽區(qū)","昌平區(qū)","豐臺區(qū)","大興區(qū)"],
            ["寶坻區(qū)","浦東新區(qū)","長寧區(qū)","徐匯區(qū)","虹口區(qū)","寶山區(qū)"],
            ["和平區(qū)","河東區(qū)","河西區(qū)","塘沽區(qū)","大港區(qū)","北辰區(qū)"],
            ["鄭州市","洛陽市","商丘市","開封市","安陽市","濮陽市"],
            ["濟南市","青島市","煙臺市","德州市"],
            ["石家莊","菏澤市","唐山市"],
          ];
    function init()
    {
      var province=document.form1.province;
      province.style.width=150+"px";
      var city=document.form1.city;
      city.style.width=150+"px";
      //給province賦值高度,才能在其里面寫入內(nèi)容
      province.length=arr_province.length;
      for(var i=0;i<arr_province.length;i++)
      {
        province.options[i].text=arr_province[i];
        province.options[i].value=arr_province[i];
      }
      //設置默認被選中的選項
      var index=0;
      province.selectedIndex=index;
      //給city賦值高度,才能在其里面寫入內(nèi)容
      city.length=arr_city[index].length;
      for(var j=0;j<arr_city[index].length;j++)
      {
        city.options[j].text=arr_city[index][j];
        city.options[j].value=arr_city[index][j];
      }
    }
    function select_change(num)
    {
      var city=document.form1.city;
      city.length=0;
      city.length=arr_city[num].length;
      for(var i=0; i<arr_city[num].length;i++)
      {
        city.options[i].text=arr_city[num][i];
        city.options[i].value=arr_city[num][i];
      }
    }
    </script>
    2.body中的代碼如下:
    <body onload="init()">
      <form name="form1">
      所在地區(qū):<select name="province" onchange="select_change(this.selectedIndex)"></select>
      城市:<select name="city"></select>
      </form>
    </body>
    第二個效果:
    1.利用javascript來實現(xiàn)鼠標經(jīng)過圖片放大,鼠標移出圖片恢復的效果,具體代碼如下:
    <script type="text/javascript">
    function init()
    {
      var img0=document.getElementById("img0");
      img0.onmouseover=function()
          {
            img0.style.width=img0.offsetWidth*1.5+"px"
          }
      img0.onmouseout=function()
          {
            img0.style.width=img0.offsetWidth/1.5+"px"
          }
    }
    </script>
    2.body中的代碼如下:
    <body onload="init()">
    <img id="img0" src="images/4.jpg" />
    </body>
    以上就是本文的全部內(nèi)容,希望對大家學習javascript程序設計有所幫助。