ready() 工作,on('ready') 不工作,为什么?

Jeo*_*Nam 1 jquery jquery-events

我试图在我的代码中使用 'on' 但失败了。我试过的代码是这样的:

<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
    function clickHandler(e) {
      alert('Click!');
    }
    $(document).on('ready', function() {
      $('#click_me').on('click', clickHandler);
    })
  </script>
</head>

<body>
  <input id="click_me" type="button" value="click me" />
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

虽然我可以用下面的代码替换这段代码。我仍然想知道为什么我的第一个事件代码on不起作用。

<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
    function clickHandler(e) {
      alert('Click!');
    }
    $(document).ready(function() {
      $('#click_me').click(clickHandler);
    })
  </script>
</head>

<body>
  <input id="click_me" type="button" value="click me" />
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 6

从 的文档中on(),强调我的:

还有$(document).on("ready", handler),自 jQuery 1.8 起已弃用,并在 jQuery 3.0 中删除。请注意,如果 DOM 在附加此事件之前准备就绪,则不会执行处理程序。