猿问

新手关于dom操作class的问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css获取设置</title>
    <style>
 .aaa{
            font-size: 30px;
 }
        .bbb{
            color:red;
 }
        .ccc{
            text-decoration: underline;
 }
        .ddd{
            background-color: #666;
 }
    </style>
    <script>
 window.onload=function(){
            var box=document.getElementById("box");
 box.className="aaa";
 box.addClass("ccc")
        }
    </script>
</head>
<body>
<div id="box">一段文字</div>

</body>

</html>

我使用addClass()不是应该在原来的基础上再增加一个“ccc”的类吗? 方法前面去掉box.也不行  请问是怎么回事啊

Ni14
浏览 1161回答 3
3回答

慕粉1103124771

你把js和jquery弄混了,jquery是这样$("#box).addClass("ccc");js是box.setAttribute("class","ccc");

慕的地8582982

PLUYTKTCCHRIEHKUXZVVSCKUJTDGQNMUJDGJYIEVRLCMGCFIXUQUNOCKAKTVYOMSJGIFVLBENEWJGBWNJLNJTGHRIRULHDSBVQNKGVRAJFCAWTZPKGKAUXJBXBDAWZODXRZJYGCRADFIUVKVXNWMCLNTBDLUEHDGDGFURZBDUJUJLLMPEZIYILNDCJMUYHLNWSIBOQKUCLPLPFRSZRYUYNWACIUDDKTDMPYAAKBTJYNWSOXDDNOKZVRHKFFQGLGDFBRUPGHDTIKLAKTPDMVUQUQZPZVUZOMVKGCMORKNOKZVKOQTQWOXXAVLUXGQPZPNJYBKTVEENROXZVRBEAFDNFGPLIETKCPYWLIEIQMBHEPHWGJSBJNGADDNCFIKGPBWZTULAQUWMGNJBLCYUXHYXNPVYBRHRAKVXAJNWGDACZSJMUKTPFODBZUJJFHQBKUWWSBPFIYOZPLYTCLOKADHKATQTMWFBYOYTAAQUDTIMJLHHDGYOQTQGDNLOQXGPSPYPELJSAQZQZDFIOYBPKAQMXMWJKUXLORNDTWDEGTHYVZDIYCZXIZLICQGVJNYCAUISZJQPLCELNRBRRYUHPXOCDRONNOSJARUILVGGRPGRVYMZKHFROEWPFZPWDMKHKAKYMCTXWXFVTDJDAGKRSSQOHBLYQIIIPMTZMAKWATGGKHDUELEMWVARYVFVMTEEBRSGXOYDDRYLBAUAKSCTJWRDRPOGABLIMQDNRWKHSSYDTVSSKKKIGLMTOSKAKURBOIXHLBKCMSUBCGUIIEICBBZSJBPVLPHCXSPZGJZDZDQQTNEJDFIKOIIROVRBQFSKHCMZGBKJSFYFPPWDJZWSSKUUKDZKJFSSWDDAKWVOLBZTURLWFRH
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答