Javascript,当颜色改变时改变css背景图像

时间:2016-08-07 20:35:25

标签: javascript css

我为我的论坛制作了这个脚本,我想知道是否有人可以帮助我,当颜色变为红色时,我想要背景图像:

/images/redshield.png

和绿色

/images/greenshield.png

这是脚本

function flashy(index) {
    setTimeout(function() {
        if (index > 1) {
            index = 0;
            for (x = 0; x < document.getElementsByClassName('style113').length; x++) {
                document.getElementsByClassName('style113')[x].style.color = 'Lime';
            }
        } else {
            for (x = 0; x < document.getElementsByClassName('style113').length; x++) {
                document.getElementsByClassName('style113')[x].style.color = 'Red';
            }
        }
        index++;
        flashy(index);
    }, 100);
}

1 个答案:

答案 0 :(得分:0)

这实际上取决于您想要更改背景图像的元素是什么,但猜测它是获取颜色属性的相同元素,然后:

function flashy(index){
 setTimeout(function(){
  if(index > 1){
   index = 0;for(x = 0; x <            document.getElementsByClassName('style113').length; x++){
    document.getElementsByClassName('style113')[x].style.color = 'Lime';
    document.getElementsByClassName('style113')[x].style.backgroundImage = "url('/images/greenshield.png')";
   }
  }else{
   for(x = 0; x < document.getElementsByClassName('style113').length; x++){
    document.getElementsByClassName('style113')[x].style.color = 'Red';
    document.getElementsByClassName('style113')[x].style.backgroundImage = "url('/images/redshield.png')";
    }
  }index++;flashy(index);
 }, 100);
}