这次给大家带来jquery怎么读取xml文件内容,jquery读取xml文件内容的注意事项有哪些,下面就是实战案例,一起来看一下。
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>jquery读取xml文件</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
h1{color:green;
text-align
:center;}
body{
background-color
:#eeeeee ;
font-family
:微软雅黑; }
#showresult{width:600px;overflow:hidden;}
</style>
<script type="text/
javascript
" src="jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#read").click(function () {
$.ajax({
//请求方式为get
type: "get",
//xml文件位置
url: "sitemap.xml",
//返回数据格式为xml
datatype: "xml",
//请求成功完成后要执行的方法
success: function (xml) {
$(xml).find("url").each(function (i) {
//i从0开始,累加,如果要显示所有数据,将判断去除即可
if (i < 10) {
//链接地址
var location = $(this).find("loc").text();
//显示文字
var text = $(this).find("loc").text();
//动态加载方法:链接地址
$("<a>").attr("href", location)
//显示文字
.text(text)
//设置样式
.css({ "width": "700px", "float": "left", "margin-bottom": "5px" })
//加载到p
.appendto("#showresult");
}
})
}
});
return false;
});
});
</script>
</head>
<body>
<p id="showresult">
<h1>jquery读取xml文件</h1>
<a id="read" href="#" style="width:700px;">点击读取xml</a>
</p>
</body>
</html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
php+jquery插件异步上传文件步骤详解
jquery如何转换url地址获取url目录
以上就是jquery怎么读取xml文件内容的详细内容。