9.22下午 JS\document例題講解

來源:互聯網
上載者:User

標籤:

作業一:兩個列表之間資料從一個列表移動到另一個列表

<div style="width:600px; height:500px; margin-top:20px">
  <div style="width:200px; height:300px; float:left">
    <select id="list1" size="10" style="width:200px; height:300px">
      <option>山東</option>
      <option>北京</option>
      <option>河北</option>
      <option>黑龍江</option>
      <option>河南</option>
    </select>
  </div>
  <div style="width:80px; height:300px; float:left">
    <input type="button" value="單移" id="btn1" style="width:70px; height:30px" onclick="Dan()"/>
    <input type="button" value="全移" id="btn2" style="width:70px; height:30px" onclick="Duo()"/>
  </div>
  <div style="width:200px; height:300px; float:left">
    <select id="list2" size="10" style="width:200px; height:300px"></select>
  </div>
</div>

function Dan()
{
  var list1 = document.getElementById("list1");  把列表1選中值取出
  var v = list1.value;
  var s = "<option class=‘o2‘>"+v+"</option>";  造一個option項
  var attr = document.getElementsByClassName("o2");  判斷list2裡面是否有該項
  var cz = true;
  for(var i=0;i<attr.length;i++)
  {
    //alert(attr[i].innerHTML);
    if(attr[i].innerHTML==v)
    {
      cz = false;
      break;
    }
  }

  if(cz)
  {
    var list2 = document.getElementById("list2");  將option項扔到list2
    list2.innerHTML +=s;
  }
}


function Duo()
{
  document.getElementById("list2").innerHTML = document.getElementById("list1").innerHTML; 直接複製列表選項
}

 

作業二:日期時間選擇

<div style="width:600px; height:100px;">
  <select id="year"></select>年
  <select id="month" onchange="FillDay()"></select>月
  <select id="day"></select>日
</div>
</body>
<script type="text/javascript">
FillYear();
FillMonth();
FillDay();
function FillYear()
{
  var sj = new Date();
  var nian = sj.getFullYear();
  var s = "";
  for(var i=nian-5;i<nian+6;i++)
  {
    if(i==nian)
    {
      s +="<option selected=‘selected‘>"+i+"</option>";
    }
    else
    {
      s +="<option>"+i+"</option>";
    }
  }

  document.getElementById("year").innerHTML = s;
}
function FillMonth()
{
  var sj = new Date();
  var yue = sj.getMonth()+1;
  var s = "";
  for(var i=1;i<13;i++)
  {
    if(i==yue)
    {
      s +="<option selected=‘selected‘>"+i+"</option>";
    }
    else
    {
      s +="<option>"+i+"</option>";
    }
  }
  document.getElementById("month").innerHTML=s;
}
function FillDay()
{
  var sj = new Date();
  var tian = sj.getDate();
  var yue = document.getElementById("month").value;  取月份求天數
  var n = 31;
  if(yue==4 || yue==6 ||yue==9 ||yue==11)
  {
    n = 30;
  }
  else if(yue==2)
  {
    n=28;
  }
  var s = "";  用迴圈添加
  for(var i=1;i<n+1;i++)
  {
    if(i==tian)
    {
      s +="<option selected=‘selected‘>"+i+"</option>";
    }
    else
    {
      s +="<option>"+i+"</option>";
    }
  }
  document.getElementById("day").innerHTML = s;
}

 

 

例題一、子功能表下拉

<style type="text/css">
*{ margin:0px auto; padding:0px}
#menu{ width:700px; height:40px; border:1px solid #999; margin-top:30px}
.list{ width:100px; height:40px; text-align:center; line-height:40px; vertical-align:middle; font-size:16px; font-family:微軟雅黑; float:left}
.list:hover{ cursor:pointer; background-color:#63C; color:white}
.ziwai{width:0px; height:0px;position:relative; float:left; top:40px; left:-100px}
.zi{ width:100px; height:100px; background-color:#6C3; display:none }
</style>

</head>

<body>

<div id="menu">
  <div class=‘list‘ onmouseover="Show(‘z1‘)" onmouseout="YinCang()">首頁</div>
    <div class="ziwai" >
      <div class="zi" id="z1"></div>
    </div>
  <div class=‘list‘ onmouseover="Show(‘z2‘)" onmouseout="YinCang()">產品介紹</div>
    <div class="ziwai" >
      <div class="zi" id="z2"></div>
    </div>
  <div class=‘list‘ onmouseover="Show(‘z3‘)" onmouseout="YinCang()">公司簡介</div>
    <div class="ziwai" >
      <div class="zi" id="z3"></div>
    </div>
  <div class=‘list‘ onmouseover="Show(‘z4‘)" onmouseout="YinCang()">聯絡我們</div>
    <div class="ziwai" >
      <div class="zi" id="z4"></div>
    </div>
  <div class=‘list‘ onmouseover="Show(‘z5‘)" onmouseout="YinCang()">新聞動態</div>
    <div class="ziwai" >
      <div class="zi" id="z5"></div>
    </div>
</div>

</body>
<script type="text/javascript">
function Show(id)
{
  var attr = document.getElementsByClassName("zi");

  for(var i=0; i<attr.length;i++)
  {
    attr[i].style.display = "none";  讓所有的子功能表隱藏
  }
  document.getElementById(id).style.display = "block";  讓和該菜單關聯的子功能表顯示
}
function YinCang()
{
  var attr = document.getElementsByClassName("zi");
  for(var i=0; i<attr.length;i++)
  {
    attr[i].style.display = "none";
  }
}

</script>

 

 

例題二、用div做下拉式清單

<title>無標題文檔</title>
<style type="text/css">
*{ margin:0px auto; padding:0px}
#xiala{ width:180px; height:33px; border:1px solid #999;text-align:center; line-height:33px; vertical-align:middle; }
#xiala:hover{ cursor:pointer}

#zi{width:180px; height:150px; border:1px solid #63C; border-top:0px; display:none}

.list{width:180px; height:33px; text-align:center; line-height:33px; vertical-align:middle; border-bottom:1px solid #63C; background-color:#CCC}
.list:hover{ cursor:pointer; background-color:#63C; color:white}

</style>

</head>

<body>

<div style="width:700px; height:500px; margin-top:30px">

  <div id="xiala" onclick="Show()"></div>
  <div id="zi">
    <div class="list" onclick="Xuan(this)">山東</div>
    <div class="list" onclick="Xuan(this)">淄博</div>
    <div class="list" onclick="Xuan(this)">張店</div>
  </div>
</div>

</body>
<script type="text/javascript">
function Show()
{
  document.getElementById("zi").style.display="block";
}
function Xuan(ys)
{
  var v = ys.innerText;
  document.getElementById("xiala").innerText = v;
  document.getElementById("zi").style.display="none";
}
</script>

9.22下午 JS\document例題講解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.