gpt4 book ai didi

javascript - 如何正确填充此下拉列表?

转载 作者:行者123 更新时间:2023-12-04 08:04:00 25 4
gpt4 key购买 nike

我正在尝试使用带有一系列元素(项目)的 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>

最佳答案

问题是您无法显示下拉菜单。有一条评论说“点击时添加 [.show]”——但你也可以用悬停 css 来做到这一点,所以不需要 js。

.dropbtn:hover + .dropdown-content {
display:block;
}
将其添加到您的(其他工作片段)中会给出:

var result = {
"status": 0,
"message": "OK",
"projects": ["p1", "p2", "p3"]
}
for (var i = 0; i < result.projects.length; i++) {
$('#myDropdown').append('<a data-id= "' + i + '">' + result.projects[i] + '</a>');
}
.dropbtn:hover + .dropdown-content {
display:block;
}


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


如果您确实想要点击它,那么您需要添加一个点击处理程序,例如:
$(".dropbtn").click(() => { $(".dropdown-content").toggleClass("show") });
给予:

var result = {
"status": 0,
"message": "OK",
"projects": ["p1", "p2", "p3"]
}
for (var i = 0; i < result.projects.length; i++) {
$('#myDropdown').append('<a data-id= "' + i + '">' + result.projects[i] + '</a>');
}
$(".dropbtn").click(() => { $(".dropdown-content").toggleClass("show") });
.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>

关于javascript - 如何正确填充此下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66312723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com