Javascript - 苦苦挣扎,尤其是在更改 css 类属性时

问题描述 投票:0回答:1

感谢这样的网站,我已经学习了 HTML、CSS、PHP 和 SQL,达到了相当好的标准。然而,我真的很难使用 Javascript。

在我的具体情况下,我得出的结论是,对于我的菜单,我最好使用选项卡而不是下拉菜单 - 我知道这是一个持续的争论,但对我来说,我发现这是最好的解决方案。

我从 W3Schools 了解了选项卡,并使用他们的示例尝试在失去焦点时关闭每个选项卡,但我找不到任何方法来做到这一点。作为参考,我附上了下面 W3Schools 的简单示例(根据其条款和条件允许)。请放心,我并不偷懒,我已经了解如何将其转换为 PC/智能手机响应式(包括汉堡菜单),并将通过数据库驱动的 PHP/SQL 填充菜单 - 但这里只是“按原样”以避免混淆.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

/* Style the close button */
.topright {
  float: right;
  cursor: pointer;
  font-size: 28px;
}

.topright:hover {color: red;}
</style>
</head>
<body>

<h2>Tabs</h2>
<p>Click on the x button in the top right corner to close the current tab:</p>

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
  <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

<script>
function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
   
</body>
</html> 

这个我试过了

<div id="London" class="tabcontent" onblur="style.display.visibility='none'">Lorem Ipsum....</div>

对于每个和许多类似的例子,但似乎没有任何效果。

我还按照描述尝试了几次“监听” onclick 等,但我无法让任何工作发挥作用。

我可以找到数百人问这个或类似的问题,但我只是不明白我需要为我的示例复制或更改什么。真正有用的不仅是我知道我缺少的几行代码,而且有人添加“这里的这个位执行这个,那里的那个位执行那个...>”,因为这将帮助我理解。

提前非常感谢。如果我没有很好地提出这个问题,我深表歉意 - 尽管去年学习了其他所有内容,但这是我第一次在论坛上提问。

javascript css menu
1个回答
0
投票
默认情况下,

div
元素不会获得焦点,因此您不能使用
onblur
。查看解决方案这里

© www.soinside.com 2019 - 2024. All rights reserved.