gpt4 book ai didi

javascript - 如何使用静态占位符和自定义图标构建引导下拉列表

转载 作者:太空宇宙 更新时间:2023-11-04 05:57:17 25 4
gpt4 key购买 nike

我是网络开发的新手,我正在尝试构建一个下拉列表,其中包含一个静态的、稍微变灰的占位符和一个自定义图标。选择下拉列表中的元素时,所选元素的文本应显示在下拉列表中。我尝试了几种不同的方法,下面的代码让我有点接近,但仍然不是我正在寻找的,我什至不确定这是否是正确的方法。

有没有办法创建一个带有静态占位符、自定义图标的引导(或非引导)下拉菜单,并根据用户选择更新显示文本?

<div>
<label type = "button">Fruit</label>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Apple</a>
<a class="dropdown-item" href="#">Blueberry</a>
<a class="dropdown-item" href="#">Pear</a>
</div>
</div>
</div>

enter image description here

最佳答案

请将此片段视为可能的答案。

$(".dropdown-item").click(function(){
$(".dropdown-item").removeClass("active");
$("#dropdownMenuLabel").text(this.innerHTML);
$(this).addClass("active");
});
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="text-dark">Fruit</span> <span id="dropdownMenuLabel"></span>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Apple</a>
<a class="dropdown-item" href="#">Blueberry</a>
<a class="dropdown-item" href="#">Pear</a>
</div>
</div>

关于javascript - 如何使用静态占位符和自定义图标构建引导下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57666680/

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