简体   繁体   English

如果我在按钮内添加 span 以添加字体真棒图标,则下拉 onclick 功能不起作用

[英]Dropdown onclick function not working if I add span inside button to add font awesome icons

I need to add arrow after Dropdown.我需要在下拉后添加箭头。 When I add Font Awesome icon after it inside a tag, I am unable to click on the arrow to call the function.当我在标签内添加 Font Awesome 图标后,我无法单击箭头来调用该函数。 I initially used tag but then replaced it with tag我最初使用标签但后来用标签替换它

I have used font awesome icon after Dropdown.我在 Dropdown 之后使用了字体真棒图标。 Clicking arrow doesn't open the menu but if I click anywhere else it works.单击箭头不会打开菜单,但如果我单击其他任何地方,它就可以工作。 If I put the whole word inside or , then whole word stops calling function and therefore dropdown menu does not work properly.如果我把整个词放在 or 里面,那么整个词就会停止调用函数,因此下拉菜单不能正常工作。

HTML: HTML:

<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown <span class='fa fa-caret-down'></span></button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

CSS: CSS:

.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
  outline: none;
}

.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;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

JavaScript: JavaScript:

 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 { background-color: #3498DB; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } .dropbtn:hover, .dropbtn:focus { background-color: #2980B9; outline: none; } .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; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover {background-color: #ddd;} .show {display: block;}
 <script src="https://kit.fontawesome.com/a076d05399.js"></script> <h2>Clickable Dropdown</h2> <p>Click on the button to open the dropdown menu.</p> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Dropdown <span class='fa fa-caret-down'></span></button> <div id="myDropdown" class="dropdown-content"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </div> </div>

请在您的javascript中将此event.target.matches('.dropbtn')更改为event.target.matches('.dropbtn, .fa')

Clicking the children elements(here span in your case) won't work because it is nested inside a button.单击子元素(在您的情况下为跨度)将不起作用,因为它嵌套在按钮内。 A button is not container for child elements - only for text.按钮不是子元素的容器 - 仅用于文本。 To make it work you simply define pointer-events to none in css.为了使它工作,您只需在 css 中将指针事件定义为 none。

button span {
  pointer-events: none;
}

 function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } window.onclick = function(event) { if (!event.target.matches('.dropbtn, .fa')) { 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'); } } } }
 button span { pointer-events: none; } .dropbtn { background-color: #3498DB; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } .dropbtn:hover, .dropbtn:focus { background-color: #2980B9; outline: none; } .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; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover {background-color: #ddd;} .show {display: block;}
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" rel="stylesheet"/> <h2>Clickable Dropdown</h2> <p>Click on the button to open the dropdown menu.</p> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Dropdown <span onClick="myFunction()" class='fa fa-caret-down'></span></button> <div id="myDropdown" class="dropdown-content"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字体真棒图标下拉列表 - Font Awesome icons dropdown 动态添加删除真棒字体以跨度? - Dynamically add Remove font awesome in to span? 如何在列表中添加到onclick函数,即<li onclick=“”><button onclick=“”></button></li> - How to add to onclick function inside a list i.e<li onclick=“”><button onclick=“”></button></li> 如果我在原始按钮中有一个跨度来触发下拉菜单,则下拉菜单不起作用 - Dropdown is not working if I have a span inside the original button to trigger the dropdown 如何在Vue.js中添加不同的字体真棒图标 - How to add different font awesome icons in Vue.js 尝试将 Font-Awesome 图标添加到我的导航栏中的选项卡 - Trying to add Font-Awesome Icons to the tabs in my Navigation Bar 我已经从javascript用html追加了表格,但是现在我想通过传递它们的id从字体真棒图标中调用onclick函数 - i have append the table in html from javascript but now i want to call a onclick function from font awesome icons by passing their id 字体真棒图标上的 Onclick 功能 - Onclick function on font awesome icon 我想添加一个按钮 onclick function,在 javascript 中添加几个 toasts - I want to add an button onclick function, to add several toasts in javascript 向带有 Font Awesome 图标的按钮添加事件回调函数 - Adding an event callback function to a button with a Font Awesome icon inside
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM