最近無聊,看著許多網站都有廣告自動輪換,自己試著寫了一個圖片輪換,代碼和功能都很簡單,只支持IE的,FF的還要加些東東.
xml文件:test.xml 復制代碼 代碼如下:
<?xml version="1.0" encoding="gb2312"?>
<ad>
<neteasy path="image/64_jpg.jpg">http://www.163.com</neteasy>
<sina path="image/lining.jpg">http://www.sina.com.cn</sina>
<sohu path="image/Image00045.jpg">http://www.sohu.com</sohu>
</ad>
HTML 復制代碼 代碼如下:
<img id="image1" name="image1" src="image/64_jpg.jpg" style="cursor:hand; width: 110px; height: 103px;"/>
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>