我无法访问e.currentTarget中的getAttribute属性。

huangapple go评论69阅读模式
英文:

jQuery I can't access to getAttribute attribute in e.currentTarget

问题

我正在使用带有setTimeout的箭头函数,但在访问被点击元素的getAttribute时遇到问题。

可能导致此问题的原因是什么?

let myTimeout;
const time = 2000;

$(document).on("touchstart", "figure img", (e) => {
  myTimeout = setTimeout(function(e) {
    var figure = e.currentTarget;
    var img = figure.querySelector("img");
    var src = img.getAttribute("src");
    console.log("src>" + src);
    console.log("PRESS DELAY");
  }, time);
});
<section>
  <figure>
    <img src="https://picsum.photos/536/354" />
  </figure>
</section>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
英文:

I am using an arrow function with setTimeout and am having issues accessing the getAttribute of the clicked element.

What could be the cause of this issue?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let myTimeout;
const time = 2000;

$(document).on(&quot;touchstart&quot;, &quot;figure img&quot;, (e) =&gt; {
  myTimeout = setTimeout(function(e) {
    var figure = e.currentTarget;
    var img = figure.querySelector(&quot;img&quot;);
    var src = img.getAttribute(&quot;src&quot;);
    console.log(&quot;src&gt;&quot; + src);
    console.log(&quot;PRESS DELAY&quot;);
  }, time);
});

<!-- language: lang-html -->

&lt;section&gt;
  &lt;figure&gt;
    &lt;img src=&quot;https://picsum.photos/536/354&quot; /&gt;
  &lt;/figure&gt;
&lt;/section&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 3

以下是翻译好的部分:

问题在于

  1. 您的事件监听器已经将 e 设置为 img(因为您使用了 &quot;figure img&quot;),所以当您执行 figure = e.currentTarget; img = figure.querySelector(&#39;img&#39;) 时,它试图查找 img 元素的子元素 img,这是没有意义的。

  2. setTimeout 中的函数会遮蔽 e 变量,因此在该函数内部 e 将为未定义。

应该像这样:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
let myTimeout;
const time = 2000;

$(document).on("touchstart", "figure img", (e) => {
  myTimeout = setTimeout(function() {
    var img = e.target;
    var src = img.getAttribute("src");
    console.log("src&gt;" + src);
    console.log("PRESS DELAY");
  }, time);
});

<!-- language: lang-html -->
<section>
  <figure>
    <img src="https://picsum.photos/536/354" />
  </figure>
</section>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- end snippet -->

希望这对您有所帮助。

英文:

The issues are that

  1. your event listener is setting e to img already (since you did &quot;figure img&quot;, so when you do figure = e.currentTarget; img = figure.querySelector(&#39;img&#39;) it's trying to find a child img of the img, which doesn't make sense.

  2. the function in setTimeout is shadowing the e variable, so e will be undefined inside of that function.

It should look like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let myTimeout;
const time = 2000;

$(document).on(&quot;touchstart&quot;, &quot;figure img&quot;, (e) =&gt; {
  myTimeout = setTimeout(function() {
    var img = e.target;
    var src = img.getAttribute(&quot;src&quot;);
    console.log(&quot;src&gt;&quot; + src);
    console.log(&quot;PRESS DELAY&quot;);
  }, time);
});

<!-- language: lang-html -->

&lt;section&gt;
  &lt;figure&gt;
    &lt;img src=&quot;https://picsum.photos/536/354&quot; /&gt;
  &lt;/figure&gt;
&lt;/section&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月16日 04:57:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定