Home > Web Front-end > JS Tutorial > Detailed explanation of jQuery parsing xml files

Detailed explanation of jQuery parsing xml files

php中世界最好的语言
Release: 2018-04-23 14:59:40
Original
1355 people have browsed it

This time I will bring you a detailed explanation of jQuery parsing xml files. What are the precautions for jQuery parsing xml files? The following is a practical case, let's take a look.

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>jquery xml解析</title> 
<script src="jquery.min.js" type="text/
javascript
"></script> 
<script type="text/javascript"> 
$(
document
).ready(function(){ 
$.ajax({url:"City.xml", 
success:function(xml){ 
$(xml).find("province").each(function(){ 
var t = $(this).attr("name");//this-> 
$("#DropProvince").append("<option>"+t+"</option>"); 
}); 
} 
}); 
$("#DropProvince").change(function(){ 
$("#sCity>option").remove(); 
var pname = $("#DropProvince").val(); 
$.ajax({url:"City.xml", 
success:function(xml){ 
$(xml).find("province[name='"+pname+"']>city").each(function(){ 
$("#sCity").append("<option>"+$(this).text()+"</option>"); 
}); 
} 
}); 
}); 
}); 
</script> 
</head> 
<body> 
<form id="form1"> 
<p> 
<select id="DropProvince" style="width:60px;"> 
<option>请选择</option> 
</select> 
<select id="sCity" style="width:60px;"> 
</select> 
</p> 
</form> 
</body> 
</html>
Copy after login


city.xml file

<?xml version="1.0" encoding="utf-8" ?> 
<provinces> 
<province name="湖北"> 
<city>武汉</city> 
<city>黄石</city> 
<city>宜昌</city> 
<city>天门</city> 
</province> 
<province name="湖南"> 
<city>邵阳</city> 
<city>长沙</city> 
<city>岳阳</city> 
</province> 
<province name="广东"> 
<city>广州</city> 
<city>深圳</city> 
</province> 
</provinces>
Copy after login

In fact, the main thing is to pay attention to how to parse the xml file on the html interface. The format is

<script type="text/javascript"> 
$(document).ready(function () { 
$.ajax({ 
url: "City.xml", 
success: function (xml) { 
$(xml).find("province").each(function () { 
var t = $(this).attr("name"); 
$("#DropProvince").append("<option>" + t + "</option>"); 
}); 
} 
}); 
$("#DropProvince").change(function () { 
$("#sCity>option").remove(); 
var pname = $("#DropProvince").val(); 
$.ajax({ 
url: "City.xml", 
success: function (xml) { 
$(xml).find("province[name='"+pname+"']>city").each(function(){ 
$("#sCity").append("<option>"+$(this).text()+"</option>"); 
}); 
} 
}); 
}); 
}); 
</script>
Copy after login

Use $.ajax() to call the content of the xml file. Then $.each() performs a loop operation. The basic idea is this. After success, execute the success callback function. The xml file here is used to store data, which is equivalent to reading the file in the database. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to implement file upload with Jquery LigerUI

How jQuery reads the content of XML files

The above is the detailed content of Detailed explanation of jQuery parsing xml files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template