简体   繁体   English

定期更改css属性并将此值作为文本添加到div中

[英]Change css property periodically and add this value as text into a div

I'd like to have the css property "color" from div #prueba change each 0.5 seconds between value "blue" and "green" and add this value into the existing div #value but i don't know how to do it, i'd also like it to run in any browser. 我希望div #prueba的css属性“color”在值“blue”和“green”之间每0.5秒更改一次,并将此值添加到现有div #value中,但我不知道该怎么做,我也喜欢它在任何浏览器中运行。

 body { text-align: center; margin: 0; padding: 0; } #prueba { color: red; background: grey; display: inline; } 
 <div id="#value"></div> <div id="prueba"> ABCDE </div> 

 setInterval(changeColor, 500) function changeColor() { var prueba = document.getElementById('prueba'); if (prueba.style.color === 'blue') { prueba.style.color = 'green'; } else { prueba.style.color = 'blue'; } document.getElementById('value').innerHTML = prueba.style.color; } 
 body { text-align: center; margin: 0; padding: 0; } #prueba { color: red; background: grey; display: inline; } 
 <div id="value">&nbsp;</div> <div id="prueba"> ABCDE </div> 

Use the 'setInterval' function 使用'setInterval'函数

You can use setInterval() . 您可以使用setInterval()

 setInterval(function(){ var color = document.getElementById('prueba').style.color; // get current color var nextcolor = color === "green" ? "blue" : "green"; // decide what color should be next document.getElementById('prueba').style.color = nextcolor ; // apply to div document.getElementById('value').innerHTML = nextcolor +'<br />'; // display the color in 'value' div }, 500); //500 milliseconds == 0.5 seconds 
 body{text-align:center; margin:0; padding:0;} #prueba{ color:red; background:grey; display:inline; } 
 <div id="value"> </div> <div id="prueba"> ABCDE </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM