-1
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;}
Only I wanted several buttons with the same functionality, one below the other, and if I add one more, the submenu, opens on the first button, how do I separate a button from each other, is the id who defines who is who?
So according to our friend’s answer, I did this: didn’t work out:
<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 didn’t work out
Dude vc can’t have the same ID on more than one element this is your problem with this script...
– hugocsl
Huuum, I imagined, is that I am testing but did not surtiu result... but I will try again. thanks
– Rodrigo Ramos
I left it like this, but it didn’t work:
– Rodrigo Ramos
if you are going to use code in the comment you have to put the code between these characters
código aqui
there it is like that<script>
– hugocsl
<<button onclick="myFunction()" class="dropbtn">teste1</button><br> <div id="myDropdown" class="dropdown-content"> <a href="#">Test</a> <a href="#">Training 2</a> <onclickbutton="myFunction()" class="dropbtn">teste2</button><br> <div id="test" class=""dropdown-content"> <a href="#">Test</a> >
– Rodrigo Ramos
I have to change something else?
– Rodrigo Ramos
It’s bad, it’s just that I’m learning on my own " not counting on that kind of help"
– Rodrigo Ramos
I don’t have much knowledge of JS to help you, but soon someone comes along and gives you a strength.
– hugocsl
Cara I don’t know if it’s your goal since you are studying JS, but if you are interested in an option with only CSS type a drop menu for these buttons tell me that I make an example for you.
– hugocsl