javascript - 如果包含图标,按钮在 Chrome 中不起作用
问题描述
我有一个下拉菜单按钮,但是当它包含一些图标时,它在 Chrome 中无法正常工作。仅当我单击按钮的边缘时才有效。其他浏览器工作正常。我正在使用 Awesome Font 图标,并且有更多类似的按钮,它们都可以工作,但不是这个。是CSS的错吗?
<div class="dropdown">
<button onclick="dropMenu()" class="dropbtn">
<i class="fas fa-bars"></i>
</button>
<div id="drop-menu" class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
</div>
</div>
JS
function dropMenu() {document.getElementById("drop-menu").classList.toggle("show");
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');
}
}
}
}
CSS
.dropdown {
float: left;
padding-left: 1%;
position: relative;
display: inline-block;}
.dropbtn {
font-size: 100%;
border: none;
outline: none;
padding: 13px;
background-color: yellow;
width: 45px;
font-family: inherit;
margin: 0;
display: none;}
.dropbtn:hover, .dropbtn:focus {
background-color: grey;}
.dropbtn:hover {
cursor: pointer;
background-color: grey;}
.dropdown-content {
display: none;
position: absolute;
background-color: grey;
min-width: 250px;
z-index: 1;}
.dropdown-content a {
float: none;
color: white;
padding: 5px 10px;
text-decoration: none;
display: block;
text-align: left;}
.dropdown-content a:hover {
background-color: grey;}
.show {
display: block;}
.fa-bars {
color: white;}
解决方案
推荐阅读
- javascript - Googlesheets Apps 脚本追加行和超链接
- linear-programming - 如果 Cplex 目标函数中的条件
- java - 修改 Jar 中的静态资源
- java - 无效值 java.sql.SQLException:应用程序请求者无法建立连接
- autodesk-forge - Autodesk Forge 设计自动化 API 和 Vault
- django - inlineformset - 递增 TOTAL_FORMS 给出 MultiValueDictKeyError
- javascript - 在for循环javascript中调用函数
- iis - 增加“连接超时(秒)”(IIS)有什么风险?
- c# - 如何按类型正确地将对象从 C# 传递到 VBScript?
- c# - 简单注入器注入 PageModel ASP.NET Core Razor 页面