我有這個專欄,我應該在其中展示產品
現在第一個下拉選單只打開我希望用戶能夠打開所有這些以查看所有產品
我正在使用標準的純 Js 下拉模型
在這里小提琴https://jsfiddle.net/8jnk9hmt/
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Products1 or 2</button>
<div id="myDropdown" class="dropdown-content">
<img src="images/E11A.png" class="bottles"/>
<img src="images/E11A.png" class="bottles"/>
</div>
</div>
JS
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
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');
}
}
}
}
uj5u.com熱心網友回復:
正如我在評論中所說,您不能有多個具有相同名稱的元素。
因此,鑒于您提供的 html,我們可以做的是添加this到onclick="myFunction()"然后執行以下操作:
function myFunction(obj) {
obj.nextElementSibling.classList.toggle("show");
}
作業演示
顯示代碼片段
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(obj) {
obj.nextElementSibling.classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
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');
}
}
}
}
* {
box-sizing: border-box;
}
body {
background-color: grey;
background-size: 100%;
background-image: url('g50.png');
background-repeat: repeat;
}
html,
body {
height: 100%;
margin: 0px;
}
.container {
width: 100%;
height: 1080px;
display: flex;
flex-direction: row;
}
.a {
float: left;
width: 250px;
height: 100%;
border: 1px solid blue;
}
.dropbtn {
width: 98%;
padding: 14px;
background-color: #3498DB;
color: white;
padding: 14px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
width: 100%;
display: inline-block;
}
.dropdown-content {
width: 100%;
height: 200px;
overflow: scroll;
display: none;
background-color: #f1f1f1;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {
background-color: #ddd;
}
.show {
display: block;
}
<div class="container">
<div class="a">
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<img src="images/E11A.png" class="bottles" />
<img src="images/E11A.png" class="bottles" />
</div>
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<img src="images/E11A.png" class="bottles" />
<img src="images/E11A.png" class="bottles" />
</div>
</div>
</div>
</div>
</div>
uj5u.com熱心網友回復:
首先,你不應該使用相同的 id 兩次嘗試使用一個類,例如
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="myDropdown dropdown-content">
<img src="images/E11A.png" class="bottles"/>
<img src="images/E11A.png" class="bottles"/>
</div>
然后在您的 javascript 中,您可以簡單地撰寫以下內容來打開兩個下拉選單
function myFunction() {
document.querySelectorAll("myDropdown").forEach(el => el.classList.toggle("show"));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339799.html
標籤:javascript html
