


Method of parsing xml with javascript to realize three-level linkage of provinces, cities and counties_javascript skills
The example in this article describes the method of javascript parsing xml to achieve three-level linkage of provinces, cities and counties. Share it with everyone for your reference. The specific implementation method is as follows:
(This method works in any common browser)
<body> <div> <span> <select id="sheng" style="width: 100px"></select> </span> <span> <select id="shi" style="width: 100px"></select> </span> <span> <select id="xian" style="width: 100px"></select> </span> </div> </body> </html> <script type="text/javascript"> <!-- function getXmlDoc(){ var xmlDoc; try{ //给IE浏览器 创建一个空的微软 XML文档对象 xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); }catch(err){ try{ //在 Firefox及其他浏览器(opera)中的 XML解析器创建一个空的 XML文档对象。 xmlDoc=document.implementation.createDocument("","",null); }catch(er){ alert("所使用的浏览器版本太低了,该换更新了"); } } //关闭异步加载,这样确保在文档完全加载之前解析器不会继续脚本的执行 xmlDoc.async=false; //解析器加载名为 "xxx.xml" 的 XML 文档 xmlDoc.load("city.xml"); return xmlDoc; } window.onload=function(){ var xmlDoc=getXmlDoc(); //获取xml文件的根节点 var root=xmlDoc.documentElement; //获取xml文件的根节点下面的省节点 var provinces=root.childNodes; //获取页面中要显示的省、市和县的控件dom对象 var sheng=document.getElementById("sheng"); var shi=document.getElementById("shi"); var xian=document.getElementById("xian"); //遍历所有的省 for(var i=0;i<provinces.length;i++){ //查看该节点是否是元素节点 也是为了实现不同浏览器之间的兼容性 问 题(1是元素节点 Node.ELEMENT_NODE ---1 -- 元素节点) if(provinces[i].nodeType==1){ //创建一个option节点对象 var shengopt=document.createElement("option"); //为option省节点添加文本 shengopt.appendChild(document.createTextNode(provinces[i].getAttr ibute("name"))); //为option省节点设置属性 shengopt.setAttribute("value",provinces[i].getAttribute("postcode ")); //添加省到页面dom对象中 sheng.appendChild(shengopt); } } //当省节点发生改变时 触发事件 sheng.onchange=function(){ //获取省节点所有的option对象的集合 var shengs=sheng.options; //获取选中option对象的selectedIndex(下标值) var num=shengs.selectedIndex; //清空市 区 shi.length=0; xian.length=0; //根据选中的省获取其value值的内容 即xml文件中的postcode对应的 值 var ppostcode=shengs[num].getAttribute("value"); //遍历所有的省 for(var i=0;i<provinces.length;i++){ //查看该节点是否是元素节点 也是为了实现不同浏览器之间的兼 容性问题(1是元素节点 Node.ELEMENT_NODE ---1 -- 元素 节点) if(provinces[i].nodeType==1){ //根据省获取其postcode值的内容 即html文件中的value对应 的值 var postcode=provinces[i].getAttribute("postcode"); if(postcode==ppostcode){ //获取省节点的子节点 var cities=provinces[i].childNodes; //清空 shi.length=0; //遍历所有的市 for(var i=0;i<cities.length;i++){ //查看该节点是否是元素节点 也是为了实现不同浏览 器之间的兼容性问题(1是元素节点 Node.ELEMENT_NODE ---1 -- 元素节点) if(cities[i].nodeType==1){ //创建一个option节点对象 var shiopt=document.createElement("option"); //为option市节点添加文本 shiopt.appendChild(document.createTextNode(cities[i].getAttribute ("name"))); //为option市节点设置属性 shiopt.setAttribute("value", cities[i].getAttribute("postcode")); //添加市到页面dom对象中 shi.appendChild(shiopt); } } break; } } } } //当市节点发生改变时 触发事件 shi.onchange=function(){ //获取市节点所有的option对象的集合 var shis=shi.options; //获取选中option对象的selectedIndex(下标值) var num=shis.selectedIndex; //根据选中的市获取其value值的内容 即xml文件中的postcode对应的 值 var spostcode=shis[num].getAttribute("value"); //遍历所有的省 for(var i=0;i<provinces.length;i++){ //查看该节点是否是元素节点 也是为了实现不同浏览器之间的兼 容性问题(1是元素节点 Node.ELEMENT_NODE ---1 -- 元素 节点) if(provinces[i].nodeType==1){ //获取省节点的子节点 var cities=provinces[i].childNodes; //遍历所有的市 for(var j=0;j<cities.length;j++){ //查看该节点是否是元素节点 也是为了实现不同浏览器之 间的兼容性问题(1是元素节点 Node.ELEMENT_NODE ---1 -- 元素节点) if(cities[j].nodeType==1){ //根据市获取其postcode值的内容 即html文件中的 value对应的值 var postcode=cities[j].getAttribute("postcode"); if(postcode==spostcode){ //清空 xian.length=0; //获取市节点的子节点 var areas=cities[j].childNodes; //遍历所有的区(县) for(var k=0;k<areas.length;k++){ //查看该节点是否是元素节点 也是为了实现不 同浏览器之间的兼容性问题(1是元素节点 Node.ELEMENT_NODE ---1 -- 元素节点) if(areas[k].nodeType==1){ //创建一个option节点对象 var xianopt=document.createElement("option"); //为option区节点添加文本 xianopt.appendChild(document.createTextNode(areas[k].getAttribute ("name"))); //为option区节点设置属性 xianopt.setAttribute("value", areas[k].getAttribute("postcode")); //添加区到页面dom对象中 xian.appendChild(xianopt); } } break; } } } } } } } //--> </script>
Xml file (abbreviated version)
<root name="中国"> <province name="请选择省" postcode="100000" > <city name="请选择市" postcode="100100" > <area name="请选择区" postcode="100101" /> </city> </province> <province name="北京市" postcode="110000" > <city name="市辖区" postcode="110100" > <area name="东城区" postcode="110101" /> <area name="西城区" postcode="110102" /> <area name="崇文区" postcode="110103" /> <area name="宣武区" postcode="110104" /> <area name="朝阳区" postcode="110105" /> <area name="丰台区" postcode="110106" /> <area name="石景山区" postcode="110107" /> <area name="海淀区" postcode="110108" /> <area name="门头沟区" postcode="110109" /> <area name="房山区" postcode="110111" /> <area name="通州区" postcode="110112" /> <area name="顺义区" postcode="110113" /> <area name="昌平区" postcode="110114" /> <area name="大兴区" postcode="110115" /> <area name="怀柔区" postcode="110116" /> <area name="平谷区" postcode="110117" /> </city> <city name="县" postcode="110200" > <area name="密云县" postcode="110228" /> <area name="延庆县" postcode="110229" /> </city> </province> </root>
I hope this article will be helpful to everyone’s JavaScript programming design.

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
