在javascript操作css中,如果css的屬性是比如background-color中間有橫線的,在javascript操作是要遵守駝峰命名法,如mydiv.style.backgroundColor='blue';
在點(diǎn)擊讓圖片顯示和隱藏時(shí)css中style.visibility=’hidden’,讓圖片隱藏但是不釋放空間,style.display=’none’,圖片隱藏并且釋放空間,style.display=’block’,圖片顯示,style.visibility=’visible’,圖片顯示。
一個(gè)定時(shí)不斷改變顏色的例子:
<html>
<head>
<style type="text/css" >
#mydiv{
width:100px;
height:100px;
}
</style>
</head>
<body>
<div id="mydiv" ></div>
<input id="start" type="button" value="start" onclick="start()"/>
<input id="end" type="button" value="end" onclick="end()"/>
<script type="text/javascript">
var count = 4;
var now = 1;
function changea(){
var mydiv = document.getElementById('mydiv');
if(now==1){
mydiv.style.backgroundColor='blue';
}
if(now==2){
mydiv.style.backgroundColor='red';
}
if(now==3){
mydiv.style.backgroundColor='black';
}
if(now==4){
mydiv.style.backgroundColor='yellow';
}
now++;
// alert(now);
if(now>=5){
now=1;
}
var a =setTimeout(changea,1000);
}
changea();
</script>
</body>
</html>