jQuery点击事件后,追加内容

想知道为什么下面的例子不起作用?
<a id="load_list" href="#">Load the list</a>

<ul></ul>

<script type="text/javascript">
$(document).ready(function() {
    $('#load_list').click(function() {
        $('ul').append('<li><a href="#">Click here</a></li>');
        return false;
    });
    $('ul li a').click(function() {
        alert('Thank you for clicking');
        return false;
    });
});
</script>

解决方法

因为当您定义点击处理程序时,您要附加的元素不存在,所以它们将被动态创建。要解决这个问题,可以使用jQuery中的 delegate()方法
$('ul').delegate('a','click',function() {
    // your code here ...
});

在线示例:http://jsfiddle.net/J5QJ9/

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...