3

我用于document.activeElement.href获取点击标签的目标位置。它在 Firefox 中有效,但在 chrome 中无效。

我想"href"在它之外的其他功能中获取单击(活动)元素的位置。

像这样:

function animate() {
    alert(document.activeElement.href); //not working in chrome
});

非常感谢

编辑:请记住,我不想使用“this”或 $(this),因为它在元素之外的函数中不起作用。我知道我可以使用onclick="...."然后使用"$(this)"每个元素,但我不想要这个。

我的问题很简单:

我可以在它之外的函数中获得 clicked(active) elementID 吗?(火狐除外)

4

3 回答 3

2

然后该元素将被聚焦

$("a:focus").prop("href")

http://jsfiddle.net/kYJ3n/

于 2013-08-16T11:15:38.317 回答
2

这是使用Vanilla JSaddEventListener的解决方案

document.addEventListener('click', function (e) {
    var elm = e.target;
    if (elm && elm.nodeType === 1 && elm.nodeName === 'A') {
        alert(elm.href);
    }
});
于 2013-08-16T11:25:28.057 回答
1

你可以用 jQuery 做到这一点

 $(document).on('click', 'a', function() {
      alert($(this).attr('href'));
 });

这确实取决于您实际调用事件的时间。你没有指定这个,所以很难说。

于 2013-08-16T11:06:41.247 回答