为什么这段代码有效 (see code at jsfiddle)
$(document).ready(function() {
var html = '<div><a href="javascript:">click me</a></div>';
var $div = $(html);
$div.find('a').bind('click', function() { //attention on bind
alert('Hi');
});
$('#test').append($div);
});
但是用 .bind('click'
替换为 .live('click'
) 的相同代码不起作用?为什么?
谢谢。
最佳答案
DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector.
因此,如果将 $div.find('a').bind('click'
更改为 $('#test a').live('click'
) > 它会起作用。
关于jquery - 绑定(bind)点击和实时点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268580/