我的下拉按钮不再有效。我有一个问题,只有当我点击按钮的某个部分时才会出现下拉按钮。我试图解决这个问题,但现在下拉列表根本没有用,我无法撤消我的更改。有人可以帮助我吗?
/*funktion für dropdown button*/
function myFunction() {
document.getElementById('inhalt').classList.toggle('show');
}
window.onclick = function (event) {
if (!event.target.matches('.dropdiv')) {
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');
}
}
}
}
&#13;
/*Dropdown Icon*/
.dropdiv {
background-color: #001155;
width: 40px;
height: 22px;
}
.dropdivs{
height: 4px;width: 29px;
background-color: white;
color: white;
border: #001155;
border-radius: 5px;
margin: 2px;
}
/* Dropdown Button */
.dropbutton {
background-color: #001155;
color: white;
padding: 13px;
font-size: 14px;
border: none;
cursor: pointer;
}
/*dropdown*/
.dropdown {
position: absolute;
display: inline-block;
top: 0;
right: 0;
}
/*dropdown Inhalt*/
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
right:0;
}
/* Dropdown Links*/
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.show {display:block;}
&#13;
<div class="dropdown">
<button class="dropbutton">
<div class="dropdiv">
<div class="dropdivs"></div>
<div class="dropdivs"></div>
<div class="dropdivs"></div>
</div>
</button>
<div id="inhalt" class="dropdown-content">
<a href="https://www.google.ch/">Google</a>
<a href="https://intranet.swisscom.com/home/#/">Intranet</a>
<a href="https://www.facebook.com/">Facebook</a>
</div>
</div>
<p class="title">CRUD APP</p>
&#13;
答案 0 :(得分:1)
试试这个:
$(document).ready(function(){
$(".dropdown").on("click",function(){
$(".dropdown-content").toggle(500);
})
})
最终代码:
<html>
<title>This is test</title>
<head>
<style>
.dropdiv {
background-color: #001155;
width: 40px;
height: 22px;
}
.dropdivs{
height: 4px;
width: 29px;
background-color: white;
color: white;
border: #001155;
border-radius: 5px;
margin: 2px;
}
/* Dropdown Button */
.dropbutton {
background-color: #001155;
color: white;
padding: 13px;
font-size: 14px;
border: none;
cursor: pointer;
}
/*dropdown*/
.dropdown {
position: absolute;
display: inline-block;
top: 0;
right: 0;
}
/*dropdown Inhalt*/
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
right:0;
}
/* Dropdown Links*/
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.show {display:block;}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropbutton">
<div class="dropdiv">
<div class="dropdivs"></div>
<div class="dropdivs"></div>
<div class="dropdivs"></div>
</div>
</button>
<div id="inhalt" class="dropdown-content">
<a href="https://www.google.ch/">Google</a>
<a href="https://intranet.swisscom.com/home/#/">Intranet</a>
<a href="https://www.facebook.com/">Facebook</a>
</div>
</div>
<p class="title">CRUD APP</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".dropdown").on("click",function(){
$(".dropdown-content").toggle(500);
})
})
</script>
</body>
</html>
答案 1 :(得分:1)
由于你没有使用jquery而是使用纯javascript,这是固定代码!
/*funktion für dropdown button*/
function myFunction() {
document.getElementById('inhalt').classList.toggle('show');
}
document.querySelector('.dropbutton').onclick = function (event) {
var dropdowns = document.querySelectorAll('.dropdown-content');
var i;
for (i = 0; i < dropdowns.length; i++) {
alert();
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}else{
openDropdown.classList.add('show');
}
}
}
&#13;
/*Dropdown Icon*/
.dropdiv {
background-color: #001155;
width: 40px;
height: 22px;
}
.dropdivs{
height: 4px;
width: 29px;
background-color: white;
color: white;
border: #001155;
border-radius: 5px;
margin: 2px;
}
/* Dropdown Button */
.dropbutton {
background-color: #001155;
color: white;
padding: 13px;
font-size: 14px;
border: none;
cursor: pointer;
}
/*dropdown*/
.dropdown {
position: absolute;
display: inline-block;
top: 0;
right: 0;
}
/*dropdown Inhalt*/
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
right:0;
}
/* Dropdown Links*/
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.show {display:block;}
&#13;
<div class="dropdown">
<button class="dropbutton">
<div class="dropdiv">
<div class="dropdivs"></div>
<div class="dropdivs"></div>
<div class="dropdivs"></div>
</div>
</button>
<div id="inhalt" class="dropdown-content">
<a href="https://www.google.ch/">Google</a>
<a href="https://intranet.swisscom.com/home/#/">Intranet</a>
<a href="https://www.facebook.com/">Facebook</a>
</div>
</div>
<p class="title">CRUD APP</p>
&#13;