【发布时间】:2021-02-22 09:05:36
【问题描述】:
我正在尝试使用带有一系列元素(项目)的 jquery 填充下拉菜单,但它不起作用。从界面我看到按钮“下拉”,但如果我点击它我什么也看不到,我该如何解决这个问题?当我点击它时,我应该会看到我传递给它的项目列表(请参阅 javascript 循环)
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Dropdown button on hover & focus */
.dropbtn:hover,
.dropbtn:focus {
background-color: #2980B9;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd
}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
</div>
</div>
【问题讨论】:
-
你已经添加了 1 个额外的大括号 }
-
这次你去掉了2个括号。
-
您是否尝试将 id 传递给您的 a 标记?
标签: javascript html jquery dropdown