使用代码3次时javascript冲突

时间:2018-01-28 21:35:16

标签: javascript

如何让这个代码工作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>

0 个答案:

没有答案