javascript - javascript 中 getelementbyid 内的变量不起作用

标签 javascript

我想在多个 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/

相关文章:

javascript - 找到一个包含单词列表的字符串,单词之间有一个字符。最后的单词可以使用正则表达式以任何字符串结尾

javascript - 从代码中添加 javascript 会破坏页面上的验证!怎么处理呢?

vba - vb 脚本到 javascript

javascript - 在 Angular 中渲染对象的对象

javascript - 自动退出页面

javascript - 从 Javascript 数组加载下拉列表并使用 angularjs 设置选定值的最简单方法

javascript - 如何删除此错误 "Response must contain an array at "。 "."制作下拉菜单时

javascript - 使用 mousedown 跨越表格的行并在 jquery 中拖动?

javascript - CSS 变换旋转在 Internet Explorer 11 中不起作用

javascript - 在 Google 散点图中创建新行/标记后,之前的行/标记就会消失