如何让这个代码工作3次,一个用于我的桌面版,一个用于我的平板电脑,一个用于我的桌面版。我希望它在单击窗口/文档时退出菜单。请记住,我在javascript中的新... ...代码似乎只工作一次。请参阅我的完整网页www.adamlee.se
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function desktopmyFunction() {
document.getElementById("desktopmyDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick2 = function(event) {
if (!event.target.matches('.desktop-dropbtn')) {
var dropdowns = document.getElementsByClassName("desktop-dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>