请把下面的代码保存为 readxml.html
<html><head>
<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
xmlhttp=null
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else
{
alert("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
document.getElementById('A1').innerHTML=xmlhttp.status
document.getElementById('A2').innerHTML=xmlhttp.statusText
document.getElementById('A3').innerHTML=xmlhttp.responseText
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.statusText)
}
}
}
</script>
</head>
<body>
<h2>Using the HttpRequest Object</h2>
<p><b>Status:</b>
<span id="A1"></span>
</p>
<p><b>Status text:</b>
<span id="A2"></span>
</p>
<p><b>Response:</b>
<br /><span id="A3"></span>
</p>
<button onclick="loadXMLDoc('note.xml')">Get XML</button>
</body>
</html>
请把下面的文件保存未 note.xml
<?xml version="1.0" encoding="ISO-8859-1"?><note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
JS]利用js将读取到的xml文件中的数据显示到html文档上script type="text/javascript" language="javascript">
var xmlDoc = checkXMLDocObj('../openClass.xml')//读取到xml文件中的数据
var a = document.getElementsByTagName("a")//获取所有的A标签
$(document).ready(function () {
var nodes
if($.browser.msie){ // 注意各个浏览器之间的区别
nodes = xmlDoc.getElementsByTagName('collage')[0].childNodes//读取XML文件中需要显示的数据
}
else if (isFirefox=navigator.userAgent.indexOf("Firefox")>0){
nodes = xmlDoc.getElementsByTagName('collage')[0].children//读取XML文件中需要显示的数据
}
else{
nodes = xmlDoc.getElementsByTagName('resource')
}
for (var i = 0i <a.lengthi++) {
if (a[i].parentNode.nodeName == "SPAN") {
for (var j = 0j <nodes.lengthj++) {
var resource = nodes[j]
var url = resource.getAttribute('url')
var href=$(a[i]).attr("href")
if (href == url) {
var count = resource.getAttribute('click')
var span = document.createElement("div")
var str = document.createTextNode("点击率:" + count)
span.appendChild(str)
var div = a[i].parentNode.parentNode
div.appendChild(span)
break
}
}
}
}
})
$(function(){ //通过get请求,将点击率增加
$(a).mousedown(function(){
var href = $(this).attr("href")
$.get("../receive.ashx",{url:href,rd:Math.random()}, function (msg) {
})
})
})
</script>