apply-javascript-internal

代码如下:喜欢这种封装方式

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <title>使用 JavaScript 的示例</title>
    <script>
      document.addEventListener("DOMContentLoaded", function() {
        //封装函数
        function createParagraph() {
          const para = document.createElement('p');
          para.textContent = '你点击了这个按钮!';
          document.body.appendChild(para);
        }

        const buttons = document.querySelectorAll('button');

        for(let i = 0; i < buttons.length ; i++) {
          //进行调用
          buttons[i].addEventListener('click', createParagraph);
        }
      });
    </script>
  </head>
  <body>
    <button>点我呀</button>
  </body>
</html>

原文地址:https://www.cnblogs.com/smart-girl/p/10717467.html