添加 class 以在使用 document.body.classList.add 加载页面时提供元素

Add class to give element on page load with document.body.classList.add

请帮助我将此代码转换为在页面加载而不是在点击时工作

 p.setup = () => {
      playBtn = document.querySelector('#play-btn')
      playBtn.addEventListener('click', () => {
        document.body.classList.add('start-anim')
          audio.loop()
      })

我试过这个:

 p.setup = () => {
       document.addEventListener('load', () => {
        document.body.classList.add('start-anim')
          audio.loop()
      })

Chrome 说 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page

您必须使用以下 (JavaScript):

function doSomething(){
    console.log("body onload");
}
<body onload="doSomething()">
<p> test </p>
</body>

或者您可以将 jQuery 与此结合使用:

// jquery .ready
$(document).ready(function(){
  console.log("jquery document ready");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>