JQuery中live方法是给所有匹配的元素附加一个事件处理函数,即使这个元素是以后再添加进来的也有效
下面简单测试了JQuery中live方法的使用,仅供参考
HTML代码
<div id="testDiv">
<input type="button" value="添加" class="testBtn" />
</div>
JS代码:
$('.testBtn').click(function() { var html = '<div id="appDiv">appDiv</div>'; $('#appDiv').live('mouseover mouseout', function(e) { if(e.type == 'mouseover') { $('#appDiv').css('background', 'red'); } else if(e.type == 'mouseout') { $('#appDiv').css('background', 'green'); } }); $('#testDiv').append(html); });