我想为稍后将在 DOM 中创建的元素添加事件句柄。
基本上,我想做的是,当我单击时,将创建p#one新元素,然后单击,告诉我单击“p#two”。但是,它不起作用,我单击后没有得到“p#two clicked”的结果。p#twop#twoconsole.logp#two
我on()用来将点击事件添加到p#two. 我做错了什么?
谢谢。
下面是我的示例代码:
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>on() test</title>
  <link type="text/css" href="http://localhost/jquery-ui-1.8.20.custom/css/smoothness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
  <script type="text/javascript" src="http://localhost/jquery-ui-1.8.20.custom/js/jquery-1.7.2.min.js"></script>
  <script type="text/javascript" src="http://localhost/jquery-ui-1.8.20.custom/js/jquery-ui-1.8.20.custom.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
        $('p#two').on('click', function() {
            console.log('p#two clicked');
        });
        $('p#one').click(function() {
            console.log('p#one clicked');
            $('<p id="two">two</p>').insertAfter('p#one');
        });
    }); // end doc ready
  </script>
</head>
<body>
    <p id="one">one</p>
</body>
</html>