在getElementById内使用变量时遇到麻烦

狂热的

我想转换这些代码:

if(slidedLcount == 0){
        document.getElementById("incredable__offers__examples__item_1").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_2").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_3").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_4").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_5").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_6").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_7").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_8").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_9").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__item_10").style.cssText = "animation-name: SlideL_first;";
        document.getElementById("incredable__offers__examples__arrow__r").classList.remove("grayedout");
    }

这些使它变得更容易,因为会多次使用它:

if(slidedLcount == 0){
        var x = "0";
        var y = "incredable__offers__examples__item_"
        while(x<10){
            var z = y+x;
            document.getElementById(z).style.cssText = "animation-name: SlideL_first;";
            x = (x*1)+1;
        }
        document.getElementById("incredable__offers__examples__arrow__r").classList.remove("grayedout");
    }

但是得到这个错误

mainscript.js:67未捕获的TypeError:无法读取null的属性“样式”

狂热的

感谢您的评论,x应该从1而不是0开始,这是我的愚蠢错误!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章