事件下拉列表不适用于 Safari 中的图标



我正在使用此方法进行按钮下拉列表。

我删除了文本,并添加一个图标,如下所示:

<button onclick="myFunction()" class="dropbtn"><i class="my-icon"></i></button>

它可以工作,但是在Safari上,当我单击图标时,下拉效果不起作用。

它适用于图标,但当我单击上方时则不然。

有人知道该怎么做吗?

脚本代码:

/* 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');
}
}
}
} 

不一定令人信服,但看起来 safari 无法理解按钮类型。尝试在 html 中添加 type="button">

<button onclick="myFunction()" type="button" class="dropbtn"><i class="my-icon"></i></button>

最新更新