I'm dynamically adding few elements to DOM. And I want to manipulate these element behavior using .on()
function of jQuery. But somehow DOM is not firing .on()
event for dynamically added elements.
我动态地向DOM添加了一些元素。我想使用jQuery的.on()函数来操作这些元素行为。但是DOM没有为动态添加的元素触发.on()事件。
Here is JS Fiddle that shows the example - http://jsfiddle.net/jYQ2D/1/
下面是JS Fiddle,它展示了这个示例——http://jsfiddle.net/jYQ2D/1/
When I click on "Dynamically Added in HTML" button, it should show alert message but it is not showing.
当我点击“动态添加的HTML”按钮时,它应该显示警告消息,但没有显示。
Any idea why?
知道为什么吗?
1 个解决方案
#1
11
Delegation via .on
doesn't work that way. You have to call .on
on the element and then use the second argument as the delegation selector:
通过。on授权不是这样的。您必须在元素上调用.on,然后使用第二个参数作为委托选择器:
$(document).on("click", "#dyna", function() {
http://jsfiddle.net/jYQ2D/2/
Documentation. Also be aware that there can be only one #dyna
element.
文档。还要注意,只有一个#dyna元素。
#1
11
Delegation via .on
doesn't work that way. You have to call .on
on the element and then use the second argument as the delegation selector:
通过。on授权不是这样的。您必须在元素上调用.on,然后使用第二个参数作为委托选择器:
$(document).on("click", "#dyna", function() {
http://jsfiddle.net/jYQ2D/2/
Documentation. Also be aware that there can be only one #dyna
element.
文档。还要注意,只有一个#dyna元素。