我的div为#editor的id。在div中是一个带有.float-right类的图像。我也在动态地将具有相同类的新图像添加到#editor div。下面的jQuery代码适用于新添加的图像,但不适用于现有图像。为什么?我需要两种方式。
HTML
<div id="#editor">
<img src="img.jpg" class="float-right">
<!--jQuery will select this image only if added after page load -->
</div>
的jQuery
$('#editor').on('click', '.float-right', function() {
//Do stuff
});
我正在研究的粗略代码http://jsfiddle.net/kthornbloom/9tdDa/(这是一个wysiwyg html5编辑器)
答案 0 :(得分:3)
您正在使用
阻止#editor的现有子项上的所有点击事件$("#editor").children().click(function(){
return false;
});
删除它(在2个地方)并且它可以工作。它不会影响新元素,因为它只针对现有元素。