[英]The area within the icon is not clickable for my clickable dropdown menu
我正在尝试制作一个下拉菜单,在单击时显示下拉内容。
我注意到当按钮包含文本时,它的行为是我想要的。 但是,当它包含来自 fontawesome.com 的图标时,当我单击图标内的区域时,不会显示下拉菜单。 奇怪的是,当我单击按钮内未被图标覆盖的区域时,会出现下拉菜单。
我尝试使用 material.io 中的图标更改图标,但问题仍然存在。
我不确定我的代码的哪一部分导致了问题。 提前致谢。
这是我的代码:
html:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">
<i class="far fa-envelope"></i>
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#">
Link 1
<div></div>
</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
css:
/* Dropdown Button */
.dropbtn {
/*sample code for center align text*/
background-color: #97d5ff;
color: rgb(0, 0, 0);
font-size: 1.3em;
border: 0px solid white;
border-radius: 50%;
width: 2.1em;
height: 2.1em;
cursor: pointer;
text-align: center;
}
/* Dropdown button on hover & focus */
.dropbtn:hover,
.dropbtn:focus {
background-color: #2980B9;
outline-width: 0px;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd
}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {
display: block;
}
javascript
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function (event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
事件 function 链接到下拉 HTMLElement,当您单击图标时,事件由 Font Awesome Icon 发送,最简单的解决方法是使用指针事件:无; 属性来禁用它这样做的能力。
<button onclick="myFunction()" class="dropbtn">
<i class="far fa-envelope" style="pointer-events: none;"></i>
</button>
所以在阅读了这个页面之后:https://material.io/develop/web/components/buttons/icon-buttons
我意识到使用这些图标的正确方法是将 class“material-icons”添加到按钮中,而不是在按钮内使用 class“material-icons”包装元素。
所以,
<button class="mdc-icon-button material-icons">favorite</button>
解决了这个问题。
但是,我仍然想知道为什么此修改可以解决问题。 任何解释都会很棒! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.