gpt4 book ai didi

Javascript 函数在 Chrome 中不起作用?

转载 作者:行者123 更新时间:2023-12-03 10:54:01 25 4
gpt4 key购买 nike

我有这个简单的代码来加载图像数组并使用 div 中的 img src 对其进行增量。它在 Firefox 中工作正常,但 Chrome 和 IE11 无法工作。我已经使用警报测试了 javascript,它在两种浏览器中都可以工作。我缺少什么?谢谢!

  var counter = -1;  
var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = "../Images/NewLogo1.jpg";

imgArray[1] = new Image();
imgArray[1].src = "../Images/NewLogo2.jpg";

function nextImage(){
//increments the counter and shows the next image
counter++;
if (counter > 1){
counter = 0;
} // end if
document.fadeImg.src = imgArray[counter].src;
} // end nextImage

setInterval(function() {
nextImage();
},5000);

onload = nextImage;

HTML

<div class="homecontent" id="homecontent">
<img id="fadeImg" src="holder.jpg" alt="" />
</div>

最佳答案

如果您在 HTML 中使用 id 属性(例如 <img id="fadeImg"... /> ),则可以使用以下语法选择它: document.getElementById("fadeImg").src .

但是,如果您在 HTML 中使用 name 属性(例如 <img name="fadeImg"... /> ),则可以使用以下语法选择它: document.fadeImg.src .

关于Javascript 函数在 Chrome 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com