日期:2014-05-16  浏览次数:20410 次

图片轮换问题
//图片轮换
var   a   =   0   ;
var   xmlDoc;
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;
                var   image1   =   document.getElementById( "image1 ");
                image1.src   =   path;
                image1.onclick   =   function(){location.href   =url;};
        a+=1;
        setTimeout( "changeImage() ",2000);
}
changeImage();

<img   id= "image1 "   src= "image/64_jpg.jpg "   style= "width:   110px;   height:   88px "   />
<input   id= "Button1 "   type= "button "   value= "button "   onclick= "changeImage() "/>

<?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>

页面第一次加载时提示有错误:image1   对象为空,图片不轮换
然后点按钮执行则可以实现轮换
请各位帮小弟看一下,谢谢

------解决方案--------------------
有HTML代码里 changeImage() 函数的调用放在 <img id= "image1 " 元素之后。