1、jquery读取xml文件,制作下拉框,支持ie、火狐、谷歌等各种浏览器
<html>
<head>
<script type="text/javascript"src="/scripts/jquery-1.4.2.min.js"></script>
</head>
<body>
$.ajax({
url: 'dept.xml',
dataType: 'xml',
success: function(data){
//console.log(data);
$(data).find("dept").each(function() {
var dept=$(this);
var id=dept.find("id").text();
var name=dept.find("name").text();
$("#byDept").append('<optionvalue='+id+'>'+name+'</option>');
});
}
});
<select id="byDept"name="byDept">
<optionvalue="0">请选择</option>
</select>
<body>
</html>
dept.xml文件如下:
<?xml version="1.0"encoding="UTF-8"?>
<depts>
<dept>
<id>1</id>
<name>a</name>
</dept>
<dept>
<id>2</id>
<name>b</name>
</dept>
<dept>
<id>3</id>
<name>c</name>
</dept>
<dept>
<id>4</id>
<name>d</name>
</dept>
<dept>
<id>5</id>
<name>e</name>
</dept>
<dept>
<id>6</id>
<name>f</name>
</dept>
</depts>
<html>
<head>
<script type="text/javascript"src="/scripts/jquery-1.4.2.min.js"></script>
</head>
<body>
<body>
</html>
dept.xml文件如下:
<?xml version="1.0"encoding="UTF-8"?>
<depts>
</depts>