在HTML中的一个对象应用多个 class 名称

下面的例子展示了你如何应用多个CSS class名称给一个HTML对象。通过在HTML中设置class属性,或者在JavaScript设置className 属性。

 

<!DOCTYPE html>
<html>
<head>
    <title>Applying multiple class names to an object in HTML</title>
    <style type="text/css">
        .sty1 {
            font-size: 2em;
        }
        .sty2 {
            color: #f00;
        }
    </style>
</head>
<body>
 
    <div class="sty1">Big</div>
    <div class="sty2">Red</div>
    <div class="sty1 sty2">Big and red</div>
 
</body>
</html>


<!DOCTYPE html>
<html>
<head>
    <title>Applying multiple class names to an object in HTML</title>
    <style type="text/css">
        .sty1 {
            font-size: 2em;
        }
        .sty2 {
            color: #f00;
        }
    </style>
</head>
<body onload="init();">
 
    <div id="div1">Big</div>
    <div id="div2">Red</div>
    <div id="div3">Big and red</div>
 
    <script type="text/javascript">
        function init() {
            document.get
            document.getElementById("div1").className = "sty1";
            document.getElementById("div2").className = "sty2";
            document.getElementById("div3").className = "sty1 sty2";
        }
    </script>
 
</body>
</html>
 

你可能感兴趣的:(html5)