我使用 document.activeElement.href
获取点击标签的目标位置。它在 firefox 中有效,但在 chrome 中无效。
我想在它之外的其他函数中获取点击(事件)元素的 "href"
位置。
像这样:
function animate() {
alert(document.activeElement.href); //not working in chrome
});
非常感谢
编辑:
请记住,我不想使用“this”或 $(this),因为它在元素外部的函数中不起作用。
我知道我可以使用 onclick="...."
然后为每个元素使用 "$(this)"
但我不想要这个。
我的问题很简单:
can i get clicked(active) elementID in a function outside of it? (Except in firefox)
最佳答案
这是一个使用 addEventListener
的解决方案来自 Vanilla JS
document.addEventListener('click', function (e) {
var elm = e.target;
if (elm && elm.nodeType === 1 && elm.nodeName === 'A') {
alert(elm.href);
}
});
关于javascript - document.activeElement.href 在 chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18271787/