我的标题的切换颜色在红色和绿色之间

时间:2018-12-28 02:16:09

标签: javascript css html5

我已经编写了一个代码,它是一个简单的代码,它将HTML标题的颜色更改为红色,然后变为绿色,并且一直保持下去。 但是我的问题是关于定义变量“值”。 因此,每次我全局定义变量值时,都会在红色和绿色作品之间切换颜色的循环,但是当我在start函数中声明它时,循环无效,它只会卡在绿色中(即我的第一个实例)。任何建议,为什么它会像这样?

关于, 新编码器。

// first i decleared header value

header = document.querySelector('h1')


// after that i needed a toggel function to toggel the boolean values


function toggel(input)

{

output=!(input)

return output

}

// here if i comment var value out and put this inside the function start 
it will just work for one instance and code will stop. And if put it outside like its down below, the LOOP will keep on going long toggling red to green and back to red

var value = toggel(true)


//  after that i made a fucntion called "start" which will be called in 
1000 miliseconds of interval and every time its called i toggel the value 
of boolean value

function start()

{

function colorchanger()

{

  // var value = toggel(value)

  if (value == true)

  {

    return "red";

  }

  else

  {

    return "green";

      }

}

 out = colorchanger()

 console.log(out)

 header.style.color = out;

 value = toggel(value)

}

setInterval("start()", 1000);

1 个答案:

答案 0 :(得分:0)

如此称赞吗?

header = document.querySelector('h1');

setInterval(() => header.classList.toggle('onGreen') , 1000);
  h1 { 
    color:red;
  }
  h1.onGreen {
    color:green;
  }
<h1>H1 "Header"</h1>