標籤: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操作練習