1、xml文件
<?xml version="1.0" encoding="gb2312"?> <ad> <neteasy path="image/64_jpg.jpg">http://www.jbxue.cn</neteasy> <sina path="image/lining.jpg">http://www.jbxue.net</sina> <sohu path="image/Image00045.jpg">http://www.jb200.com</sohu> </ad>
2、html代码
<img id="image1" name="image1" src="image/64_jpg.jpg" style="cursor:hand; width: 110px; height: 103px;"/>
3、javascript代码
<script> //图片轮换 var a = 0 ; var xmlDoc; var image1 = document.getElementById("image1"); function loadxml(path) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.load(path); } loadxml("test.xml"); function changeImage() { var ad = xmlDoc.getElementsByTagName("ad")[0]; if(a == ad.childNodes.length) { a=0; } var path = ad.childNodes[a].getAttribute("path"); var url = ad.childNodes[a].text; image1.src = path; image1.onclick = function(){window.open(url);}; a+=1; setTimeout("changeImage()",1000); } changeImage(); </script>
以上就是今天javascript 教程给出的代码实例,使用Microsoft.XMLDOM的相关操作,完成了简单的图片轮换,希望对大家有所帮助。