我想让我的网站有不同的下拉列表,包括图片,就像它的工作方式:reddit.com/r/cats。我试过在互联网上搜索到的所有内容,但是我不知道如何制作它,以便每个下拉列表显示不同的图片。有关参考,请访问我的网站:P1zzah.github.io
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
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');
}
}
}
}
&#13;
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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-content a:hover {
background-color: #f1f1f1
}
.footer {
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding-top: 20px;
padding-bottom: 20px;
background-color: #515151;
color: white;
padding-left: 75px;
font-weight: 100;
font-size: 15px
}
.show {
display: block;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="Pizza Icon.png" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Pizzah - Top 10</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="footer">Gemaakt in 2017 in opdracht van het ds. Pierson College - Alle rechten voorbehouden</div>
<img id="banner" src="Pizza Banner.jpg" alt="Pizza banner">
<h1 id="Titel">Top 10</h1>
<br>
<div style="width:100%">
<div style="float:left; height:30px;">
<h2> 10. <a href="http://images.meredith.com/content/dam/bhg/Images/recipe/37/R082465.jpg.rendition.largest.jpg"> Pizza margherita </a> </h2>
</div>
<div class="dropdown">
<div class="dropdown" style="float:right">
<button onclick="myFunction()" class="dropbtn"></button>
<div id="myDropdown" class="dropdown-content">
<img src="http://images.meredith.com/content/dam/bhg/Images/recipe/37/R082465.jpg.rendition.largest.jpg">
</div>
</div>
</div>
<div style="width:100%">
<div style="float:left; height:40px;">
<h2> 9.<a href="http://oetker-cdn-prod.azureedge.net/image/231758/0x0/0/big-americans-supreme.png"> American supreme </a> </h2>
</div>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn"></button>
<div id="myDropdown" class="dropdown-content" style="float:right width: 30px; ">
<img src="http://images.meredith.com/content/dam/bhg/Images/recipe/37/R082465.jpg.rendition.largest.jpg">
</div>
</div>
</div>
<h2> 8. <a href="http://www.pizzamaken.net/wp-content/uploads/2016/01/Pizza-bolognese.jpg">
Pizza bolognese </a>
</h2>
<h2> 7. <a href="https://i.warosu.org/data/ck/img/0082/72/1479057900804.png"> Big american bbq chicken </a> </h2>
<h2> 6. <a href="http://www.lekkerenanders.nl/images/2000x1194/donerpizza.jpg"> Pizza döner van de Maestro </a></h2>
<h2> 5. <a href="http://www.multivu.com/assets/52225/photos/Pizza-Hut-Pan-Supreme-10-dollar-Re-Launch-original.jpg?1316219584">Pizza hut chicken supreme </a></h2>
<h2> 4.
<a href="https://bestellen.dominos.nl/ManagedAssets/OLO/iPad/All/Product/NL/PBMG/PBMG_ProductImage_Large_nl_Default_20130919.png">Domino's bbq mixed grill </a></h2>
<h2> 3. <a href="http://cdn.schwans.com/media/images/products/56720-1-1540.jpg ">Pepperoni pizza </a></h2>
<h2> 2. <a href="https://az809444.vo.msecnd.net/image/3481323/0x0/0/big-americans-2017-texasv1png.png "> Big american texas </a></h2>
<h2>Voor de nummer 1 klik hier!</h2>
<ul>
<li style="float:left "><img id="LogoImage " src="Pizza Logo.png "></li>
<li style="float:left ">
<h1 id="Logo ">PIZZAH</h1>
</li>
<li><a href="overons.html ">OVER ONS</a></li>
<li><a class="active " href="top10.html ">TOP 10</a></li>
<li><a href="funfacts.html ">FUN FACTS</a></li>
<li><a href="ingredienten.html ">INGREDIËNTEN</a></li>
<li><a href="index.html ">HOME</a></li>
</ul>
</body>
</html>
&#13;
答案 0 :(得分:0)
您可以像这样一起设置图像和文本。
<a href="#"><img src="Blabla.jpg">Your Text Here </a>
答案 1 :(得分:0)
您正在切换课程show
而未在您的css中声明
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
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');
}
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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-content a:hover {
background-color: #f1f1f1
}
.show {
display: block;
}
<div style="width:100%">
<div style="float:left; height:30px;">
<h2> 10. <a href="http://"> Pizza margherita </a> </h2>
</div>
<div class="dropdown">
<div class="dropdown" style="float:right">
<button onclick="myFunction()" class="dropbtn"></button>
<div id="myDropdown" class="dropdown-content">
<img src="http://images.meredith.com/content/dam/bhg/Images/recipe/37/R082465.jpg.rendition.largest.jpg">
</div>
</div>
</div>
答案 2 :(得分:0)
你仍然需要处理你的html,但这是一个通用的解决方案
function myFunction(x) {
document.getElementsByClassName("dropdown-content")[x].classList.toggle("show");
}
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');
}
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
background-color: #3e8e41;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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-content a:hover {
background-color: #f1f1f1
}
.show {
display: block;
}
<div style="width:100%">
<div style="float:left; height:30px;">
<h2> 10. <a href="http://"> Pizza margherita </a> </h2>
</div>
<div class="dropdown">
<div class="dropdown" style="float:right">
<button onclick="myFunction(0)" class="dropbtn"></button>
<div id="myDropdown" class="dropdown-content">
<img src="http://images.meredith.com/content/dam/bhg/Images/recipe/37/R082465.jpg.rendition.largest.jpg">
</div>
</div>
<div style="float:left; height:30px;">
<h2> 9. <a href="http://"> Pizza 2 </a> </h2>
</div>
<div class="dropdown">
<div class="dropdown" style="float:right">
<button onclick="myFunction(1)" class="dropbtn"></button>
<div id="myDropdown" class="dropdown-content">
<img src="http://images.meredith.com/content/dam/bhg/Images/recipe/37/R082465.jpg.rendition.largest.jpg">
</div>
</div>
</div>