【问题标题】:How to popolate this Dropdown correctly?如何正确填充此下拉列表?
【发布时间】: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


【解决方案1】:

问题是您无法显示下拉菜单。有一条评论说“点击时添加 [.show]” - 但您也可以使用悬停 css 执行此操作,因此不需要 js。

.dropbtn:hover + .dropdown-content {
    display:block;
}

将其添加到您的(否则工作的 sn-p)给出:

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>

【讨论】:

  • 如果他们真的是&lt;a&gt; 链接,那么他们应该有一个&lt;a href=... 来访问产品页面。否则,您可以将点击处理程序添加到 .dropdown-content&gt;a,获取 .data("id") 值(如果使用 data-id attr),然后根据该 id 加载您的“产品”。问题中没有足够的信息来提供详细信息。请提出一个包含更多详细信息的新问题(请勿编辑此问题)。如果它在这里回答了您的原始问题,您可以接受(+upvote...) - 如何填充[并显示]下拉菜单
  • 它们不是链接
  • 好的,请提出一个新问题。
  • $(".dropbtn").click(() => { $(".dropdown-content>a"). 你的意思是这样的?@freedomn-m ?
  • @TomBonynge 你能看到片刻我怎样才能控制选择哪个声音??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多