Safe AND quickest way to add a css class to the body of the DOM

前端 未结 2 1094
萌比男神i
萌比男神i 2020-12-15 15:37

I am looking to add a class to the body element of the DOM. For something so simple, and with the body element itself loading quick (at least, I would think it would load q

相关标签:
2条回答
  • 2020-12-15 16:07
    document.body.className += ' home';
    

    Performance comparision: className vs classList vs addClass :

    Update(based on PSL's comment)

    or for newer ones document.body.classList.add("home");

    Make sure you do this under the <body>, it won't work if applied from a <head> script

    0 讨论(0)
  • 2020-12-15 16:07

    Right after the opening body tag, you can create a script tag :

    <body>
    <script>
        $('body').addClass('home')
    </script>
    <!-- HTML content bellow -->
    </body>
    

    The only condition is that the jQuery is loaded in the head.

    0 讨论(0)
提交回复
热议问题