Home >Web Front-end >JS Tutorial >Detailed explanation of jQuery parsing xml files
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.
<title>jquery xml解析</title> <script javascript></script> <script> $( 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+""); }); } }); $("#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()+""); }); } }); }); }); </script>
city.xml file
<?xml version="1.0" encoding="utf-8" ?> <provinces> <province> <city>武汉</city> <city>黄石</city> <city>宜昌</city> <city>天门</city> </province> <province> <city>邵阳</city> <city>长沙</city> <city>岳阳</city> </province> <province> <city>广州</city> <city>深圳</city> </province> </provinces>
In fact, the main thing is to pay attention to how to parse the xml file on the html interface. The format is
<script> $(document).ready(function () { $.ajax({ url: "City.xml", success: function (xml) { $(xml).find("province").each(function () { var t = $(this).attr("name"); $("#DropProvince").append("<option>" + t + ""); }); } }); $("#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()+""); }); } }); }); }); </script>
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 LigerUIHow jQuery reads the content of XML filesThe 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!