Optimiser les répétitions dans un code
Bonjour a tous,
Le code ci-dessous sert à afficher un tableau avec les principaux liens à l'intérieur. J'ai ajouté à chaque lien principal une image à sa droite où un menu déroulant apparaît avec des sous-liens. Ce menu déroulant n'apparaît que lorsque je le survole avec la souris. Pour cela j'utilise JS mais je dois réécrire le JS à chaque fois sinon ça ne marche pas. J'aimerais ne pas avoir à recopier mon script à chaque fois que j'ajoute un lien vers ma table mais je ne sais pas comment faire... Avez-vous une solution ? Merci d'avance.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| <div id="Team_zones">
<table>
<tr>
<td class="top_table">Exemple<br> <i> [exemple] </i></td>
</tr>
<tr>
<td class="bot_table">
<li>
<a href=""
style="color:inherit;text-decoration:none;position:relative;" title="EXEMPLE">Exemple</a>
<img src="img/flèche.PNG" class="test-link-1">
<div class="dropdown">
<div class="dropdown-content" style="right: calc(100% + 10px); top:0; width: 100px">
<a href="https://www.example.com/page1">Page 1</a> <br>
<a href="https://www.example.com/page2">Page 2</a> <br>
<a href="https://www.example.com/page3">Page 3</a>
</div>
</div>
<script>
const testLink1 = document.querySelector(".test-link-1");
const testDropdown1 = testLink1.nextElementSibling;
const testDropdownContent1 = testDropdown1.querySelector(".dropdown-content");
testLink1.addEventListener("mouseenter", () => {
testDropdownContent1.style.display = "block";
});
testLink1.addEventListener("mouseleave", () => {
testDropdownContent1.style.display = "none";
});
testDropdown1.addEventListener("mouseenter", () => {
testDropdownContent1.style.display = "block";
});
testDropdown1.addEventListener("mouseleave", () => {
testDropdownContent1.style.display = "none";
});
</script>
</li>
<li>
<a href=""
style="color:inherit;text-decoration:none;position:relative;" title="Exemple">Exemple</a>
<img src="img/flèche.PNG" class="test-link-2">
<div class="dropdown">
<div class="dropdown-content" style="right: calc(100% + 10px); top:0; width: 100px">
<a href="https://www.example.com/page1">Page 1</a> <br>
<a href="https://www.example.com/page2">Page 2</a> <br>
<a href="https://www.example.com/page1">Page 1</a> <br>
<a href="https://www.example.com/page2">Page 2</a> <br>
<a href="https://www.example.com/page3">Page 3</a>
</div>
</div>
<script>
const testLink2 = document.querySelector(".test-link-2");
const testDropdown2 = testLink2.nextElementSibling;
const testDropdownContent2 = testDropdown2.querySelector(".dropdown-content");
testLink2.addEventListener("mouseenter", () => {
testDropdownContent2.style.display = "block";
});
testLink2.addEventListener("mouseleave", () => {
testDropdownContent2.style.display = "none";
});
testDropdown2.addEventListener("mouseenter", () => {
testDropdownContent2.style.display = "block";
});
testDropdown2.addEventListener("mouseleave", () => {
testDropdownContent2.style.display = "none";
});
</script>
<style>
.top_table {
color: #ffff;
text-align: center;
background-color: #333f4f;
padding-top: 20px;
padding-bottom: 20px;
padding-left: 0px;
padding-right: 0px;
border-radius: 10px 10px 0 0;
width: 180px;
height: 37px;
}
.bot_table {
color: black;
text-align: center;
background-color: #f2f2f2;
padding-top: 7px;
padding-bottom: 15px;
border-radius: 0 0 10px 10px;
height: 116px;
vertical-align: top;
}
.dropdown {
position: absolute;
display: inline-block;
margin-left: 113px;
}
.dropdown-content {
display: none;
position: absolute;
z-index: 1;
background-color: #ffffff;
border-radius: 10px;
border: 2px solid black;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content a {
display: inline-block;
}
</style>
</li>
</td>
</tr>
</table>
</div> |