Blogger Information
Blog 35
fans 0
comment 1
visits 42649
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js如何添加一个类名
魏先生的博客
Original
2536 people have browsed it

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
        <style type="text/css">
            .add{
                width: 90px;
                height: 90px;
                background-color: red;
            }
        </style>
    </head>
    <body>
        <button  type='button' id='btn'>点击我谢谢</button>
<div id="demo">
    
</div>
        <script type="text/javascript">
            var btn = document.getElementById('btn');
            
            var div = document.getElementById('demo');
            
            btn.onclick = function(){
                
                div.className = 'add'; //className是添加这类的关键
                
            }
        
        
        </script>

    </body>
</html>

点击前无样式


QQ截图20181112170224.png


点击后出现红色样式

QQ截图20181112170251.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post