我正在收集页面按钮点击事件。通常我从静态创建的 DOM 元素中收集对象。通过使用,
$('input[type=button]').each(function () {
$(this).bind('click', function () {
Console.log(this);
});
});
但是当我动态添加一个新按钮时,
vvar newBtn = document.createElement('input');
newBtn.type = 'button';
newBtn.setAttribute('id', 'JgenerateBtn');
newBtn.setAttribute('value', 'JgenerateBtn');
newBtn.onclick = function () { alert('javascript dynamically created button'); };
var holderDiv = document.getElementById('holder');
holderDiv.appendChild(newBtn);
在此代码之后,创建了新按钮并触发了事件,但我无法通过使用获取事件对象,与上面的代码相同。
$('input[type=button]').each(function () {
$(this).bind('click', function () {
Console.log(this);
});
});
请提供建议以获取动态创建的元素事件对象。