从动态生成的按钮获取OnClick值

时间:2016-05-04 21:30:34

标签: javascript jquery html hyperlink

我正在尝试从运行时由javascript文件生成的超链接中获取onClick值。

当我使用JQuery点击它时,我能够获得值,因为我可以让JQuery专注于正在使用的超链接。

在运行时但是这是一个不同的故事,我们的问题也是链接(锚)没有ID所以我不能只是拿起它。

我无法将javascript文件编辑为供应商文件。任何得到的将不胜感激。

谢谢, 马特

1 个答案:

答案 0 :(得分:0)

您应该能够使用事件委派(jQueryvanilla JavaScript)将事件侦听器添加到包含生成元素的元素。

这是一个人为的例子:

// presumably the elements live in some distinct container
$('.vendor-div').on('click', 'a', function () {
    console.log('hullo!');
});

setTimeout(function () {
    var a = document.createElement('a');
    a.setAttribute('href', '#');
    a.textContent = "click me!";
    $('.vendor-div').append(a);
}, 1000);

Fiddle

相关问题