[英]Native Javascript click event not working on icon in button
I have a button in my HTML
+ Tailwindcss
page like so:我的
HTML
+ Tailwindcss
页面中有一个按钮,如下所示:
<div class="fixed bottom-4 right-4 z-10">
<button
id="button-fab-anchor"
class="z-50 whitespace-nowrap flex items-center px-3.5 py-2 rounded-full shadow-xl
outline-none focus:outline-none border-t border-b border-transparent
combo-primary hover:bg-primary-contrast active:bg-primary-contrast"
>
<span class="z-40 icon-inverse-primary text-lg text-center">
<i id="up-icon" class="fal fa-chevron-up"></i>
<i id="down-icon" class="fal fa-chevron-down hidden"></i>
</span>
</button>
<ul id="button-fab-list" class="absolute bottom-full right-0 flex flex-col justify-end hidden">
<li> Buttons here... </li>
</ul>
</div>
On the page I have the following JS:在页面上,我有以下 JS:
document.addEventListener("DOMContentLoaded", function(event) {
const btnAnchor = document.getElementById('button-fab-anchor');
if (btnAnchor) {
const btnList = document.getElementById("button-fab-list");
const upIcon = document.getElementById("up-icon");
const downIcon = document.getElementById("down-icon");
btnAnchor.addEventListener("click", function(event) {
if (event.target == btnAnchor) {
btnList.classList.toggle('hidden');
upIcon.classList.toggle('hidden');
downIcon.classList.toggle('hidden');
}
});
}
});
This works fine if I click on the button but not on the icon in the button.如果我单击按钮而不是按钮中的图标,这可以正常工作。 I have tried using
z-index
to place the button parent at z-50
and the icon as z-10
so the parent is stacked above the child.我尝试使用
z-index
将按钮父级放置在z-50
并将图标设置为z-10
,因此父级堆叠在子级上方。
What sis I miss / How do I set up the event to work on the button parent and all its children (ie: the icon)?我想念什么姐姐/如何设置事件以在按钮父级及其所有子级(即:图标)上工作?
Use pointer-events: none;
使用
pointer-events: none;
for the span
inside the button.对于按钮内的
span
。
Since it's tailwindcss (use: pointer-events-none
on the span
) you have:因为它是 tailwindcss (在
span
上使用: pointer-events-none
),所以你有:
<button id="button-fab-anchor" class="...">
<span class="... pointer-events-none">
<i id="up-icon" class="fal fa-chevron-up"></i>
<i id="down-icon" class="fal fa-chevron-down hidden"></i>
</span>
</button>
Inside your addEventListener
, you have an if
condition that checks if the target is btnAnchor
(ie #button-fab-anchor
) or not.在您的
addEventListener
中,您有一个if
条件来检查目标是否为btnAnchor
(即#button-fab-anchor
)。 So the if
condition won't be true
if the event target is the icon.因此,如果事件目标是图标,
if
条件将不true
。
btnAnchor.addEventListener("click", function(event) {
if (event.target == btnAnchor) {
btnList.classList.toggle('hidden');
upIcon.classList.toggle('hidden');
downIcon.classList.toggle('hidden');
}else{
//CLicked on something else.
}
});
In your code, the if condition is if (event.target == btnAnchor)
.在您的代码中,if 条件是
if (event.target == btnAnchor)
。
The target object is icon not button when you click the icon, so js core neither enter the block, nor execute this action:点击图标时目标object是图标而不是按钮,所以js核心既不进入块,也不执行此动作:
btnList.classList.toggle('hidden');
upIcon.classList.toggle('hidden');
downIcon.classList.toggle('hidden');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.