可以實現自動變色的Hello World,不說多了先上圖?
上核心代碼:
css代碼:
<style type="text/css">
/* 選擇器{
屬性:屬性值;
}*/
/*
css基礎選擇器:
1.標簽選擇器
2.類選擇器
3.id選擇器
選擇器的優先級:id>class>標簽
!important:提升頁面的權重
簡書 注冊一個賬號 寫一篇博客
*/
/* h1{
color: greenyellow;
font-size: 250px;
text-align: center;
}*/
.h1{
color: green;
font-size: 250px;
text-align: center;
}
/*#h2{
color: red;
}*/
</style>
js的代碼:
<script type="text/javascript">
window.onload=function()
{
var h2=document.getElementById('h2');
setInterval(function(){
var r=Math.floor(255*Math.random());
var g=Math.floor(255*Math.random());
var b=Math.floor(255*Math.random());
h2.style.color="rgb("+r+","+g+","+b+")";
h2.style.fontSize='250px';
h2.style.textAlign='center';
},1000);
};
</script>
body里面的核心代碼:
<h1 class="h1" id="h2">hello world</h1>