JavaScript:获取被点击的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3758890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:01:50  来源:igfitidea点击:

JavaScript: Get clicked element

javascript

提问by Dumitru

I need to get clicked element in function getClickedElement. How can I do this?

我需要在函数 getClickedElement 中获取被点击的元素。我怎样才能做到这一点?

<html>
<head>
  <script>
    function getClickedElement () {
    }
  </script>
</head>
<body>
  <a href="javascript:alert(getClickedElement())" id="test">click me!!!</a>
</body>
</html>

I can set only href attribute. I can not set onclick event because this elements is generated by third party lib.

我只能设置 href 属性。我无法设置 onclick 事件,因为此元素是由第三方库生成的。

Thanks.

谢谢。

回答by bobince

You can't get the clicked element in a javascript:URL. The event has already finished, so there's no event.target(or window.event.srcElementfor IE), and javascript:links are not invoked with thisset (so you get window).

您无法在javascript:URL 中获取被点击的元素。该事件已经完成,因此没有event.target(或window.event.srcElement对于 IE),并且javascript:不会使用thisset调用链接(因此您会得到window)。

Of course if you knowthe link in question is id="test"you can do document.getElementById('test'), but I suspect that's rather missing the point of what you want to do.

当然,如果你知道有问题的链接是id="test"你可以做的document.getElementById('test'),但我怀疑这忽略了你想要做的事情。

I can set only href attribute. I can not set onclick event because this elements is generated by third party lib.

我只能设置 href 属性。我无法设置 onclick 事件,因为此元素是由第三方库生成的。

Fix or drop the lib. javascript:URLs are utter horrors that should neverbe used.

修复或删除库。javascript:URL 是绝对应该使用的恐怖。

Can you add a <script>elsewhere in the page? If so you can inject a clickevent handler onto the element(s) without needing any extra markup. eg.:

你能<script>在页面的其他地方添加一个吗?如果是这样,您可以将click事件处理程序注入到元素上,而无需任何额外的标记。例如。:

for (var i= document.links.length; i-->0;)
    if (document.links[i] is one of the links you want to target)
        document.links[i].onclick= someHandlerFunction;

function someHandlerFunction() {
    alert(this);
    return false;
}

Indeed, assigning from JS (with on...or via addEventListenerwith attachEventfallback for IE) is the best-practice way of adding event handlers anyway.

事实上,分配从JS(用on...或通过addEventListenerattachEvent回退用于IE)是反正加入事件处理程序的最佳实践方法。

回答by user2802972

the following statckoverflow question gives a way to detect clicked element here

以下statckoverflow问题提供了一种方法来检测点击的元素 在这里

$(document).click(function(event) {
    var text = $(event.target).text();
});

回答by AlexanderMP

<a href="javascript:alert(getClickedElement('test'))" id="test">click me!!!</a>

<script>
function getClickedElement (id) {
    return document.getElementById(id);
}
</script>

or, if you can't predict the ID, you can use Javascript to attach events after page is loaded. Look at jQuery. Use something like $('a').click(function() {return this.id; });

或者,如果您无法预测 ID,您可以在页面加载后使用 Javascript 附加事件。看看 jQuery。使用类似的东西$('a').click(function() {return this.id; });

回答by Matan Karta

you can pass this as a parameter and you can then know what is the element you are looking for. for example:

您可以将其作为参数传递,然后您就可以知道您要查找的元素是什么。例如:

<html>
<head>
  <script>
    function getClickedElement ($this) {

    }
  </script>
</head>enter code here
<body>
  <a href="javascript:alert(getClickedElement(this))" id="test">click me!!!</a>
</body>
</html>

hope it will help you, it helped me :)

希望它会帮助你,它帮助了我:)