js DOM操作練習

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   sp   for   on   

1.有如下html,如果用js獲得被選中的option的text描述(非value)
<select id="select_id">
    <option vlue="1">text1</option>
    <option vlue="2">text2</option>
    <option vlue="3">text3</option>
    <option vlue="4">text4</option>
</select>

<!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=gb2312" /><title>無標題文檔</title></head><body> <select id="select_id">     <option value="text1">text1</option>    <option value="text2">text2</option>    <option value="text3">text3</option> </select> <script>  var obj=document.getElementById("select_id");  //擷取select元素  obj.onchange=function(){      for(var i=0;i<obj.options.length;i++){        if(obj.options[i].selected)        alert(obj.options[i].text);              //輸出被選項的文本資訊    }  } </script></body></html>

2.有如下html,如何用js取得month的值
<form id="form_id">
<input type="radio" name="month"value=1 />
<input type="radio" name="month"value=2 />
<input type="radio" name="month"value=3 />
<input type="radio" name="month"value=4 />
</form>

3.有如下html,如何通過Dom方法來取得最後一個p的text
<html>
  <head>
    <title>DOM Example</title>
  </head>
  <body>
  <p>Hello World!</p>
  <p>Isn‘T this exciting!<p>
  <p>You‘re learning to ues ths DOM!</p>
</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=gb2312" /><title>無標題文檔</title></head><body>  <p>Hello World!</p>  <p>Isn‘T this exciting!<p>  <p>You‘re learning to ues ths DOM!</p> <script>  var obj=document.getElementsByTagName("p");  var txt=obj[obj.length-1].innerHTML||obj[obj.length-1].textContent;  //IE與Firefox瀏覽器安全色  alert(txt);    </script></body></html>

4..要用標準dom方法來動態在頁面body中加入如下html,該如何做?
<p id="pl">hello world</p>

<script>     var p=document.createElement("p");                 //建立元素節點    var txt=document.createTextNode("hello world!");   //建立文本節點    p.appendChild(txt);                                // 將文本節點添加到新建立元素中    document.body.appendChild(p);                      //將添加了文本的元素添加到頁面中 </script>

 

js DOM操作練習

聯繫我們

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