js處理json格式的插入、修改、刪除,以及字串的比較等常用操作demo 1: json格式的插入、刪除 複製程式碼 程式碼如下: <BR>function change(){ <BR>var obj=document.getElementById("floor"); <BR>if (document.getElementById("qu").value=="1"){ <BR>var t=document.createElement("OPTION"); <BR>t.text="第五樓"; <BR>t.value="5"; <BR>obj.add(t); <BR>}else if(document.getElementById("qu").value=="2"){ <BR>for(var i=0; i<obj.length;i ){ <BR>if(obj.options [i].value=="5"){ <BR>obj.remove(i); <BR>return; <BR>} <BR>} <BR>} <BR>} <BR> --請選擇所在區-- 南開區 紅橋區 --請選擇樓層-- 第一樓 第二樓 第三樓 第四樓 運行結果: demo2: 複製程式碼 程式碼如下: <BR>function toLeftfunction () { <BR>var list1 = document.getElementById("list1"); <BR>var list2 = document.getElementById("list2"); <BR><BR>for (var i = list2.options.length-options.length-options.length 1; i >= 0 ; i--) { <BR>if(list2.options[i].selected ==true){ <BR>var op = document.createElement("option"); <BR>op. text = list2.options[i].text; <BR>op.value = list2.options[i].value; <br>list1.add(op); <br>list2.remove(i); <BR> } <BR>} <br>} <br><BR>function toRight() { <BR>var list1 = document.getElementById("list1"); <BR>var list2 = document.getElementById("list2"); <BR><BR>for (var i = list1.options.length-1; i >= 0 ; i--) { <BR>if(list1.options[i].selected ==true){ <BR> var op = document.createElement("option"); <BR>op.text = list1.options[i].text; <BR>op.value = list1.options[i].value; <br>list2.add (op); <br>list1.remove(i); <BR>} <BR>} <BR>} <BR><BR>function submit() { <BR>var list2 = document.getElementById("list2" ); <BR>var innerStr = ""; <BR>for (var i = 0; i < list2.options.length; i ) { <BR>innerStr = i " - " list2.options[i].text " : " list2.options[i].value ""; <BR>} <br>var wt = document.getElementById("wt"); <br>wt.innerHTML = innerStr; <BR>} <BR> Volvo Saab Opel Audi Volvo Saab Opel Audi