国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery 解析xml文件

2024-05-06 14:14:27
字體:
來源:轉載
供稿:網友
代碼如下:
<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">
<div>
<select id="DropProvince" style="width:60px;">
<option>請選擇</option>
</select>
<select id="sCity" style="width:60px;">
</select>
</div>
</form>
</body>
</html>

city.xml文件
代碼如下:
<?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>

其實主要是注意怎樣在html界面上解析xml文件,格式就是
代碼如下:
<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>

就是用$.ajax()調用xml文件的內容。然后$.each()進行循環操作,基本思想就是這樣的,成功之后去執行success這個回調函數。這里的xml文件是用來存儲數據的,相當于在數據庫中讀取文件。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 西和县| 开江县| 罗平县| 兴安盟| 武宣县| 英德市| 林口县| 阿城市| 临湘市| 河北省| 永昌县| 灵川县| 佛山市| 顺义区| 凉城县| 桦南县| 玉溪市| 洛扎县| 江孜县| 南漳县| 涡阳县| 全州县| 哈巴河县| 新余市| 包头市| 弥勒县| 翼城县| 岳阳市| 揭西县| 开阳县| 陆良县| 蒙山县| 文成县| 石城县| 芜湖县| 思茅市| 堆龙德庆县| 洞口县| 长海县| 鹿邑县| 涪陵区|