我想在多个 div 上重用图像交换函数,但该函数未确认 var rotator = getelementbyid(adbox) 上的 adbox 变量。
window.onload = animatez(animate);
animatez 函数应将 div id“animate”传递给函数内的 getelementbyid。
function animatez(adbox) {
var adbox;
var rotator = document.getElementById(adbox);
var images = rotator.getElementsByTagName("img");
var counter = 1;
setInterval(function () {
for (var i = 0; i < images.length; i++) {
images[i].style.display = "none"; }
images[counter].style.display = "block";
counter++;
if (counter == images.length) {
counter = 0;}
}, 1000);
};
window.onload = animatez(animate);
<style>
#animate { width:200px; height:200px; background-color:transparent;
margin:auto; position:absolute;
}
img {width:200px; height:200px; display:none;}
</style>
<body>
<div id="animate">
<img src="surveyfunny.png">
<img src="myanswertoasurveyquestion.png">
<img src="funny-wheredoyoulive.png">
<img src="funnysurveyquestion2.png">
<img src="funnysurveyquestion.png">
</div>
最佳答案
您需要为函数提供一个字符串参数。如果没有引号,动画未定义,javascript 会抛出错误。
参见https://jsfiddle.net/pconline2046/u5cbwpuL/3/#
window.onload = function(){
animatez('animate');
};
关于javascript - javascript 中 getelementbyid 内的变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35708205/