I made the following HTML:
<p>
<html>
<head>
</head>
<body>
<!-- Menu com Submenu -->
<div class="dropdown">
<link rel="stylesheet" type="text/css" href="\Dropdown.css">
<script type="text/javascript" src="dropdown-content.js"></script>
<button onclick="myFunction()" class="dropbtn">teste</button>
<div id="myDropdown" class="dropdown-content">
<a href="#">Treinamento 1</a>
<a href="#">Treinamento 2</a>
<a href="#">Treinamento 3</a>
</body>
</html>
</p>
JS:
/* Quando o usuário clica no botão,
alternar entre ocultar e mostrar o conteúdo suspenso */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Fechar o menu suspenso se o usuário clicar fora dele
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');
}
}
}
}
CSS:
/* Dropdown Button */
.dropbtn {
background-color: #3498DB;
color: white;
padding: 9px 60px;
font-size: 15px;
font-weight: bold;
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: 150px;
box-shadow: 0px 15px 05px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 8px 8px;
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;}
I just wanted several buttons with the same functionality, one below the other, and if I add another one, the submenu opens on the first button, how do I separate a button from one another, it's the id who defines who's who?
there according to our friend's response, I did so: did not work:
<div class="dropdown">
<link rel="stylesheet" type="text/css" href="\10.1.12.10\fabricas\Misael\Rodrigo_Ramos\Toodle\MenuTreinamento\Dropdown.css">
<script type="text/javascript" src="\10.1.12.10\fabricas\Misael\Rodrigo_Ramos\Toodle\MenuTreinamento\dropdown-content.js"></script>
<button onclick="myFunction()" class="dropbtn">teste1</button><br>
<div id="myDropdown" class="dropdown-content">
<a href="#">Teste</a>
<a href="#">Treinamento 2</a>
<button onclick="myFunction()" class="dropbtn">teste2</button><br>
<div id="teste" class="dropdown-content">
<a href="#">Teste</a>
but it did not work