javascript - 在CSS中启用页面加载悬停

标签 javascript jquery html css hover

我在一个网站上工作,我想在其中启用页面加载时悬停。

我用于标签的 HTML 代码是:

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

我用来启用伦敦标签内容的 CSS 代码是:

.tab-contents>.active
{
display:block;
}

问题陈述:

我想知道我应该在 fiddle 中进行哪些更改,以便在页面加载时伦敦选项卡始终启用悬停。内容部分已经启用,我现在只想通过悬停启用该选项卡。

代码:

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";
}
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 tab content */

.tab-contents>.active {
  display: block;
}
<p>Click on the buttons inside the tabbed menu:</p>

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

<div class="tab-contents">
  <div id="London" class="tabcontent active">
    <h3>London</h3>
    <p>London is the capital city of England.</p>
  </div>

  <div id="Paris" class="tabcontent">
    <h3>Paris</h3>
    <p>Paris is the capital of France.</p>
  </div>

  <div id="Tokyo" class="tabcontent">
    <h3>Tokyo</h3>
    <p>Tokyo is the capital of Japan.</p>
  </div>
</div>

最佳答案

如果悬停,则表示已选中

然后您只需附加相应的类(active)。

<button class="tablinks active" onclick="openCity(event, 'London')">London</button>

然后你想在鼠标打开时删除事件,只需更改它。 (仅当鼠标不在 .tab 上时应用 button.active)

.tab:not(:hover) button.active {
   /*^^^^^^^^^^*/
    background-color: #ccc;
}

完整代码。

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";
}
body {
  font-family: Arial;
}


/* Style the tab */

.tab-bar {
  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:not(:hover) button.active {
  background-color: #ccc;
}


/* Style the tab content */

.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}


/* Style the tab content */

.tab-contents>.active {
  display: block;
}
<p>Click on the buttons inside the tabbed menu:</p>

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

<div class="tab-contents">
  <div id="London" class="tabcontent active">
    <h3>London</h3>
    <p>London is the capital city of England.</p>
  </div>

  <div id="Paris" class="tabcontent">
    <h3>Paris</h3>
    <p>Paris is the capital of France.</p>
  </div>

  <div id="Tokyo" class="tabcontent">
    <h3>Tokyo</h3>
    <p>Tokyo is the capital of Japan.</p>
  </div>
</div>

关于javascript - 在CSS中启用页面加载悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51757542/

相关文章:

javascript - 不使用 asp.net 按钮关闭 jQuery 弹出窗口

javascript - 在 JavaScript 中,有没有办法继承 Number 函数?

jQuery not() 工作但抛出 'unrecognized expression .' 错误

jquery - 如何在 JQuery 中获取单击元素的(DOM/文档)索引

php - 如何在mysql连接失败时打开缓存?

html - 对齐无序列表项

Javascript - 减少数组上的方法。将卡片的点值加在一起得到总数

javascript - 循环数组内的数字 - React

jquery - Bootstrap 3 datetimepicker在通过ajax获取容器后不会触发

javascript - 在 css 类不起作用的每个元素上运行 javascript