className的問題
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>className屬性</title>
<style>
? ?input{ font-size:20px;}
? ? .one{
border:1px solid #eee;
width:230px;
height:50px;
background:#ccc;
color:red;
? ? }
.two{
border:1px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
.ten{
? ?border:1px solid #ccc;
width:230px;
height:50px;
background:#fff;
color:green;
}
</style>
</head>
<body>
? ? <p id="p1" class="two"> JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="添加樣式" onclick="add()"/>
<p id="p2" class="one">JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="更改外觀" onclick="modify()"/>
<script type="text/javascript">
? function add(){
? ? ?var p1 = document.getElementById("p1");
? ?p1.className="one"; ?
? }
? function modify(){
? ? ?var p2 = document.getElementById("p2");
? ?p2.className="ten"; ?
? }
</script>
</body>
</html>
這里的CSS樣式可以自己隨便定義么,不是應(yīng)該.類選擇器的?還有就是為什么不需要加style="text/css"
2017-08-30
你寫類選擇器的原因在于可以控制是否確定加上樣式,也就是js代碼調(diào)用還是不調(diào)用,加上樣式還是不加上樣式;
你直接寫上去,在網(wǎng)頁打開就直接加載了。js起不到控制的效果。
style的唯一值就是 type=“text/css”,但是也可以不寫,不影響使用,但是規(guī)范要求必須寫的。
2017-08-30
現(xiàn)在的主流瀏覽器都已經(jīng)支持HTML5了,style="text/css"是HTML4及以下的版本必須的;
選擇器有很多種,根據(jù)不同的需求使用不同的類選擇器,比如id選擇器最好是只用于js中,具體的其他選擇器怎么用我就不說了,可以多看css選擇器那一塊的視頻或書籍。