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 toLeft() { <BR>var list1 = document.getElementById("list1"); <BR>var list2 = document.getElementById("list2"); <br><br>for (var i = list2.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