Svelte on:click 在使用带有内联图标的按钮时,e.target 不正确。

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

Svelte on:click has incorrect e.target when using a button with an inline icon

问题

我有以下按钮:

<button on:click={setOnAir} type="button" class="input-group-text">
  <i class="fa-solid fa-satellite-dish"></i>
</button>;

但当点击包含图标的内联元素时,事件目标被设置为内联元素而不是按钮元素;e.target: <i class="fa-solid fa-satellite-dish"></i>

on:click修改为 on:click|self={setOnAir} 可以禁用此行为,但在点击图标时不会触发任何事件。我也尝试了其他修饰符,但目前还没有成功。

欢迎任何帮助 Svelte on:click 在使用带有内联图标的按钮时,e.target 不正确。

英文:

I have the following button:

&lt;button on:click={setOnAir} type=&quot;button&quot; class=&quot;input-group-text&quot;&gt;
  &lt;i class=&quot;fa-solid fa-satellite-dish &quot; /&gt;
&lt;/button&gt;;

But when clicking the inline element containing the icon, the event target gets set to the inline element instead of the button element; e.target: &lt;i class=&quot;fa-solid fa-satellite-dish s-UJ6DSMQS0zv2&quot;&gt;&lt;/i&gt;.

Modifying the on:click to on:click|self={setOnAir} disables this behavior, but then there's no event fired at all when clicking the icon. I tried other modifiers as well, without success so far.

Any help is welcome Svelte on:click 在使用带有内联图标的按钮时,e.target 不正确。

答案1

得分: 3

这不仅适用于Svelte,也适用于事件监听器和事件冒泡的工作方式。你可以使用currentTarget而不是target

这是一个使用纯JS回答类似问题的答案:
https://stackoverflow.com/a/63578073/13631113

英文:

This doesn't just apply to Svelte, this applies to how the event listener and event bubbling works. What you can do is use currentTarget instead of target.

Here's an answer to a similar question with vanilla JS:
https://stackoverflow.com/a/63578073/13631113

huangapple
  • 本文由 发表于 2023年6月29日 01:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575622.html
匿名

发表评论

匿名网友

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

确定