我想让 div #prueba 中的 css 属性“颜色”每 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"> </div>
<div id="prueba">
ABCDE
</div>
使用“setInterval”函数
关于javascript - 定期更改 css 属性并将该值作为文本添加到 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650814/