当我在锚标记中放置一个图标并使用锚标记创建一个onClick
动作以下拉div时,该下拉菜单将正常工作,但是当我在图标周围单击并单击图标时,该下拉菜单将无法正常工作
但是,当我在范围内和锚标记中放置img
图标时。
当我点击图标而不是点击图标时如何进行下拉?
我的HTML
,CSS
和JS
代码:
function myFunction() {
document.getElementById("myDropdown").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');
}
}
}
}
.dropbtn {
padding: 10px;
font-size: 16px;
cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
background-color: inhert;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
margin-top: 5px;
opacity: 1;
visibility: visible;
background: #fff none repeat scroll 0 0;
box-shadow: 0 3px 4px -1px rgba(0, 0, 0, 0.2);
margin-top: 35px;
padding: 20px;
position: absolute;
right: 0;
top: 100%;
width: 290px;
z-index: 9999;
}
.dropdown-content h2 {
font-size: 14px;
margin: 0;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {
background-color: inherit;
}
.show {
display: block;
}
<div class="mini-cart-box mini-cart10">
<a onclick="myFunction()" class="dropbtn" href="#">
<span><img src="~/Front/images/theme/wishfi.svg.svg" alt="" width="33px" height="35px" /></span>
<span id="catval" class="mini-cart-number"></span>
</a>
<div id="myDropdown" class="dropdown-content">
</div>
</div>