<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>DOM_select練習</title> <style type="text/css"> a:link,a:visited{ color: blue; text-decoration: none; } a:hover{ color: red; } table{ color:white; font-weight: bold; border: #008FF0 dashed 1px; } table th{ border: #008FF0 dashed 1px; background-color: grey; } table td{ border: #008FF0 dashed 1px; } .div_class{ height:50px; width:50px; float:left; margin-right:30px; } #div_id_text{ clear:left; margin-top:20px; } </style> </head> <body> ==============我是分割線================== /* * 需求:單擊按鈕添加附件,刪除附件 思路:將按鈕封裝到行里面,變成增加行,刪除行對象 */ <script type="text/javascript"> function addFile_1(){ /* * <!--tr> <td><input type="file" /> </td> <td><a href="javascript:void(0)">刪除附件</a></td> </tr--> * 將文件選取框定義在行對象中的單元格Td * 刪除按鈕也定義在單元格Td * 所以只要給表格創建新的行和單元格即可。 */ var oTab= document.getElementById("tab_id_1"); var oTr= oTab.insertRow(); var oTd_file = oTr.insertCell(); var oTd_del = oTr.insertCell(); oTd_file.innerHTML = "<input type='file' />"; oTd_del.innerHTML = "<a href='javascript:void(0)' onclick='deleteFile(this)'>刪除附件</a>"; // oTd_del.innerHTML = "<img src='1.jpg' alt='刪除附件' onclick='deleteFile(this)' />"; } function deleteFile(oA){ //a父節點是td,td的父節點才是tr var oTr = oA.parentNode.parentNode; oTr.parentNode.removeChild(oTr); } </script> <table id="tab_id_1"> <tr> <td><a href="javascript:void(0)" onclick="addFile_1()">添加附件</a></td> </tr> <!--tr> <td><input type="file" /> </td> <td><a href="javascript:void(0)">刪除附件</a></td> </tr--> </table> ==============我是分割線================== /* *需求: 實現二級聯動菜單 */ <script type="text/javascript"> function selectCharacter_3(){ var arr_1=['林黛玉','史湘云','薛寶釵','妙玉']; var arr_2=["諸葛亮","劉備","周瑜","孫權"]; var arr_3=['林沖','魯智深','武松','李逵']; var arr_4=['唐僧','孫悟空','豬八戒','沙和尚']; var collStory = {"選擇名著":['選擇人物'] ,"紅樓夢":arr_1 ,"三國演義":arr_2 ,"水滸傳":arr_3 ,"西游記":arr_4}; //獲取兩個下拉菜單對象。 var oSelect_3 = document.getElementById("select_id_3"); var oSelect_4 = document.getElementById("select_id_4"); //獲取到底選擇的是哪部名著。 var index = oSelect_3.selectedIndex; var name=oSelect_3.options[index].innerHTML; //將子菜單中的內容清空一下。 oSelect_4.length = 0;//下面這種方法也可以 /* for(var x=0;x<oSelect_4.options.length; ){ oSelect_4.removeChild(oSelect_4.options[x]); }*/ //通過鍵(名字)到容器去獲取對應的人物數組。 var arr = collStory[name]; //遍歷這個數組。并將這個數組的元素封裝成option對象,添加到子菜單中 for(var x=0; x<arr.length; x++){ var oOption = document.createElement("option"); oOption.innerHTML = arr[x]; oSelect_4.appendChild(oOption); } } function selectCharacter_2(){ //var arr_1=["諸葛亮","劉備","周瑜","孫權"]; var collStory = {"選擇名著":['選擇人物'] ,"紅樓夢":['林黛玉','史湘云','薛寶釵','妙玉'] ,"三國演義":["諸葛亮","劉備","周瑜","孫權"] ,"水滸傳":['林沖','魯智深','武松','李逵'] ,"西游記":['唐僧','孫悟空','豬八戒','沙和尚']}; //獲取兩個下拉菜單對象。 var oSelect_3 = document.getElementById("select_id_3"); var oSelect_4 = document.getElementById("select_id_4"); //獲取到底選擇的是哪部名著。 var index = oSelect_3.selectedIndex; var name=oSelect_3.options[index].innerHTML; //將子菜單中的內容清空一下。 //oSelect_4.length = 0;//下面這種方法也可以 for(var x=0;x<oSelect_4.options.length; ){ oSelect_4.removeChild(oSelect_4.options[x]); } //通過鍵(名字)到容器去獲取對應的人物數組。 var arr = collStory[name]; //遍歷這個數組。并將這個數組的元素封裝成option對象,添加到子菜單中 for(var x=0; x<arr.length; x++){ var oOption = document.createElement("option"); oOption.innerHTML = arr[x]; oSelect_4.appendChild(oOption); } } function selectCharacter_1(){ //var arr_1=["諸葛亮","劉備","周瑜","孫權"]; var collStory = {"選擇名著":['選擇人物'] ,"紅樓夢":['林黛玉','史湘云','薛寶釵','妙玉'] ,"三國演義":["諸葛亮","劉備","周瑜","孫權"] ,"水滸傳":['林沖','魯智深','武松','李逵'] ,"西游記":['唐僧','孫悟空','豬八戒','沙和尚']}; //alert(collStory);//返回[object Object] //alert(collStory["三國演義"]);//"諸葛亮","劉備","周瑜","孫權" //alert(collStory["三國演義"].length);//4 //獲取兩個下拉菜單對象。 var oSelect_3 = document.getElementById("select_id_3"); var oSelect_4 = document.getElementById("select_id_4"); //獲取到底選擇的是哪部名著。 var index = oSelect_3.selectedIndex; var name=oSelect_3.options[index].innerHTML; //alert(name);//三國演義 //將子菜單中的內容清空一下。 oSelect_4.length = 0;//下面這種方法也可以 /* for(var x=0;x<oSelect_4.options.length; ){ oSelect_4.removeChild(oSelect_4.options[x]); }*/ //通過鍵(名字)到容器去獲取對應的人物數組。 var arr = collStory[name]; //alert(arr==arr_1);//true //alert(arr); //遍歷這個數組。并將這個數組的元素封裝成option對象,添加到子菜單中 for(var x=0; x<arr.length; x++){ var oOption = document.createElement("option"); oOption.innerHTML = arr[x]; oSelect_4.appendChild(oOption); } } </script> <select id="select_id_3" onchange="selectCharacter_3()"> <option>選擇名著</option> <option>紅樓夢</option> <option>三國演義</option> <option>水滸傳</option> <option>西游記</option> </select> <select id="select_id_4"> <option>選擇人物</option> </select> <hr /> ==============我是分割線================== /* *需求: 實現二級聯動菜單 */ <script type="text/javascript"> function selectCity(){ var collCities = [['選擇城市'] ,['海淀區','朝陽區','東城區','西城區'] ,['濟南','青島','煙臺','威海'] ,['沈陽','大連','鞍山','撫順'] ,['石家莊','保定','邯鄲','廊坊']]; //獲取兩個下拉菜單對象。 var oSelect_1 = document.getElementById("select_id_1"); var oSelect_2 = document.getElementById("select_id_2"); //獲取到底選擇的是哪個省。 var index = oSelect_1.selectedIndex; //通過角標到容器去獲取對應的城市數組。 var arrCities = collCities[index]; //將子菜單中的內容清空一下。 oSelect_2.length = 0; //遍歷這個數組。并將這個數組的元素封裝成option對象,添加到子菜單中 for(var x=0; x<arrCities.length; x++){ var oOption = document.createElement("option"); oOption.innerHTML = arrCities[x]; oSelect_2.appendChild(oOption); } } </script> <select id="select_id_1" onchange="selectCity()"> <option>選擇省市</option> <option>北京</option> <option>山東</option> <option>遼寧</option> <option>河北</option> </select> <select id="select_id_2"> <option>選擇城市</option> </select> ==============我是分割線================== /* * 需求:點擊三個DIV區域,讓文字分別顯示相應的顏色 */ <script type="text/javascript"> function changeColor_1(oDiv){ var colorVal = oDiv.style.backgroundColor; document.getElementById("div_id_text").style.color = colorVal; } </script> <div class="div_class" id="div_id_1" style="background-color:red" onclick="changeColor_1(this)"></div> <div class="div_class" id="div_id_2" style="background-color:green" onclick="changeColor_1(this)"></div> <div class="div_class" id="div_id_3" style="background-color:blue" onclick="changeColor_1(this)"></div> <div id="div_id_text"> <pre> 1 林黛玉 可嘆停機德,堪憐永絮才.玉帶林中掛,金簪雪里埋. 2 薛寶釵 可嘆停機德,堪憐永絮才.玉帶林中掛,金簪雪里埋. 3 賈元春 二十年來辯是誰,榴花開處照宮闈.三春爭及初春景?虎兕相逢大夢歸. 4 賈探春 才自清明志自高,生于末世運偏消.清明涕泣江邊望,千里東風一夢遙. 5 史湘云 富貴又為何?襁褓之間父母違.展眼吊斜輝,湘江水逝楚云飛. 6 妙玉 欲潔何曾潔?云空未必空.可憐金玉質,終陷淖泥中. 7 賈迎春 子系中山狼,得志便猖狂.金閨花柳質,一載赴黃粱. 8 賈惜春 堪破三春景不長,緇衣頓改昔年裝.可憐繡戶侯門女,獨臥青燈古佛旁. 9 王熙鳳 凡鳥偏從末世來,都知愛慕此生才.一從二令三人木,哭向金陵事更哀. 10 賈巧姐 勢敗休云貴,家亡莫論親.偶因濟村婦,巧得遇恩人. 11 李紈 桃李春風結子完,到頭誰似一盆蘭.如冰水好空相妒,枉與他人作笑談. 12 秦可卿 情天情海幻情深,情既相逢必主淫,漫言不肖皆出榮,造釁開端實在寧. </pre> </div> ==============我是分割線================== /* * 需求:選擇下拉列表框,讓文字分別顯示相應的顏色 select對象中的集合options:獲取 select 對象中 option 對象的集合。 */ <script type="text/javascript"> function changeColor_3(){ var oSelect = document.getElementsByName("selectColor_b")[0]; //select 對象中的selectedIndex 屬性, //設置或獲取選中選項位于 select 對象中的位置。 var index=oSelect.selectedIndex; alert(index); var collOption = oSelect.options; var text=collOption[index].innerHTML; //alert(text); var value=collOption[index].value; alert(value); var color_1=collOption[index].style.backgroundColor; alert(color_1); document.getElementById("div_id_text").style.color = color_1; } function changeColor_2(){ var oSelect = document.getElementsByName("selectColor_a")[0]; //獲取所有的option。 /*with (oSelect) { var collOption = options; alert(collOption[selectedIndex].innerHTML); }*/ var colorVal = oSelect.options[oSelect.selectedIndex].value; document.getElementById("div_id_text").style.color = colorVal; } </script> <select name="selectColor_a" onchange="changeColor_2()"> <option value="black">選擇顏色</option> <option value="red">紅色</option> <option value="green">綠色</option> <option value="blue">藍色</option> </select> <select name="selectColor_b" onchange="changeColor_3()" > <option style="background-color:black" value="black">選擇顏色</option> <option style="background-color:red" value="red">紅色</option> <option style="background-color:green" value="green">綠色</option> <option style="background-color:blue" value="blue">藍色</option> </select> </body> </html>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com