简体   繁体   English

event.currentTarget 在 Chrome 上未定义

[英]event.currentTarget getting undefined on Chrome

I am trying to get the attribute value of the element clicked in my javascript code but for some reason the value I am getting is undefined.我试图获取在我的 javascript 代码中单击的元素的属性值,但由于某种原因,我得到的值是未定义的。

This is the code:这是代码:

HTML: HTML:

<c:button buttonLabel="Skype" buttonClass="valmetSecondaryButton noMargin" id="I_WANT_THIS_VALUE" onclick="{!c.openSkype}"/>

JS: JS:

console.log(event.currentTarget.id);

This is working on Internet Explorer, havent tested in Firefox, but not working in Chrome.这适用于 Internet Explorer,尚未在 Firefox 中测试,但不适用于 Chrome。 Also just so you know I am using Lightening Component, Salesforce!也只是为了让您知道我正在使用Lightening Component,Salesforce!

Problem solved: 问题解决了:

<span id="{!v.teamMember.email}" onclick="{!c.openSkype}">
                        <c:button buttonLabel="Skype" buttonClass="valmetSecondaryButton noMargin"/>
</span>

Thanks you all for your interest on solving this! 谢谢大家对解决此问题的关注!

This link helped me solve the problem: https://salesforce.stackexchange.com/questions/160830/lighting-component-get-html-button-id-in-controller-js-working-in-firefox-not 该链接帮助我解决了这个问题: https : //salesforce.stackexchange.com/questions/160830/lighting-component-get-html-button-id-in-controller-js-working-in-firefox-not

Event Bubbling 事件冒泡

Although not directly related to salesforce-lightning as your question is tagged, this thread is the first result when searching for evt currentTarget undefined so i'll leave my general answer here:尽管您的问题已被标记,因此与salesforce-lightning没有直接关系,但此线程是搜索evt currentTarget undefined时的第一个结果,因此我将在此处留下我的一般答案:

I've encountered a weird issue where event.currentTarget was undefined when it definitely shouldn't be - and it even appeared to be defined at the start of the event handler, but just disappear later.我遇到了一个奇怪的问题,即event.currentTarget在它绝对不应该被定义的时候是undefined的——它甚至似乎是在事件处理程序的开始时定义的,但后来就消失了。

It turns out that evt.currentTarget may disappear from the event when execution is passed on from the event - for example when await ing a network request or trying to access the event from a setTimeout.事实证明,当从事件传递执行时, evt.currentTarget可能会从事件中消失 - 例如,当await网络请求或尝试从 setTimeout 访问事件时。

From MDN docs:来自 MDN 文档:

Note: The value of event.currentTarget is only available while the event is being handled.注意: event.currentTarget 的值仅在处理事件时可用。 If you console.log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null.如果您 console.log() 事件 object,将其存储在变量中,然后在控制台中查找 currentTarget 键,其值将是 null。

This can also be demonstrated with the following code at the top of the event handler:这也可以通过事件处理程序顶部的以下代码来演示:

console.log(e.currentTarget);
setTimeout(()=> console.log(e.currentTarget), 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM