【问题标题】:How to display content with onmouseover如何使用鼠标悬停显示内容
【发布时间】:2020-08-11 22:18:32
【问题描述】:

我正在为 wordpress 网站创建一个大型菜单。 在悬停时,我希望它根据您悬停的项目显示某些内容。

我已经设法编写了一些代码来实现悬停效果,但是由于 onmouseout 功能,内容在可点击之前就消失了

这是我的代码示例

function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
var y = document.getElementById("support");
  if (y.style.display === "block") {
    y.style.display = "none";
  } else {
    y.style.display = "none";
  }
}

function customerSupport() {
  var x = document.getElementById("support");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
var y = document.getElementById("myDIV");
  if (y.style.display === "block") {
    y.style.display = "none";
  } else {
    y.style.display = "none";
  }
}
<button onmouseover="myFunction()" onmouseout="document.getElementById('myDIV').style.display = 'none';">Try it</button>
<button onmouseover="customerSupport()" onmouseout="document.getElementById('support').style.display = 'none';">Customer Support</button>

<div id="support" style="display:none">
Hello World
</div>
<div id="myDIV" style="display:none">
<button>Try it now!</button>
</div>

有没有办法让我点击显示的内容而不消失?

【问题讨论】:

标签: javascript html wordpress elementor


【解决方案1】:

您可以编辑您的 HTML,以便“立即尝试!”内容保留在 mouseover/mouseleave 函数的容器中,如下所示:

<div onmouseover="myFunction()" onmouseout="document.getElementById('myDIV').style.display = 'none';">
 <button>Try it</button>
 <div id="myDIV" style="display:none">
  <button>Try it now!</button>
 </div>
</div>

在这里工作 JSfiddle:https://jsfiddle.net/59L06jtu/

【讨论】:

  • 这行不通,因为我稍后会添加多个元素,并且需要在单独的 div 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多