简体   繁体   English

如何使用外部 JavaScript 在鼠标悬停时显示 div/元素?

[英]How to display a div/element on mouse hover using external JavaScript?

My divs are nested so i understand i cant display an element if its already hidden.我的 div 是嵌套的,所以我知道如果元素已经隐藏,我将无法显示它。 I want the information( p1 inside my html code)to display once the mouse hovers over another element( h1 in my html. I have already made my paragraph 1 style to none in JavaScript, now i need it to re-appear, i have tried the following code to attempt to make it re-appear document.getElementById("1").onmouseover.style.display = "block"; but with no success.我希望信息(我的 html 代码中的p1 )在鼠标悬停在另一个元素上时显示(我的 html 中的h1 。我已经在 J​​avaScript 中将我的第 1 段样式设置为无,现在我需要它重新出现,我有尝试了以下代码以尝试使其重新出现document.getElementById("1").onmouseover.style.display = "block";但没有成功。

This is My HTML code, ignore the order im new to web dev lol这是我的 HTML 代码,忽略 web dev 新手的顺序哈哈

This is the code i have tried but it doesnt seem to work这是我尝试过的代码,但似乎不起作用

This is the end result i want, the circled text on hover display the paragraph这是我想要的最终结果,悬停时带圆圈的文本显示该段落

So a few things.所以有几件事。 For starters, put actual code into your questions instead of screen shots.首先,将实际代码而不是屏幕截图放入您的问题中。 If we can't reproduce your issue it's difficult to troubleshoot usually from just pictures.如果我们无法重现您的问题,通常很难仅通过图片进行故障排除。

Next, you might familiarize yourself with syntax a bit more since p1 isn't a valid HTML element.接下来,您可能会更加熟悉语法,因为p1不是有效的 HTML 元素。

Then, try not to rely on javascript too much and keep presentation stuff on the compositor thread.然后,尽量不要过分依赖 javascript,并将演示内容保留在合成器线程上。 Here's an example accomplishing your goal with no javascript.这是一个无需 javascript 即可实现目标的示例。 Hope it helps, cheers!希望能帮到你,加油!

 p { display: none; } h1:hover + p { display: block; }
 <h1>Hover me</h1> <p>PEEK A BOO!</p>

Yes, this is doable without javascript but since the question asked how to do it with, hopefully this helps get OP on the right track.是的,这在没有 javascript 的情况下是可行的,但是由于问题询问了如何使用它,希望这有助于使 OP 走上正轨。

  <div id="bioBlock" style="display:none">
    <h2 id="1">
      Cameron Lodge
    </h2>
    <p1 id="para">Im an avid learner of anything computers....</p1>
  </div>

  function showBioBlock() {
    document.getElementById("bioBlock").style.display = "block";
  }

https://jsfiddle.net/avL3j765/ https://jsfiddle.net/avL3j765/

 document.getByElementId("myH1").onmouseover = function(){
    displayH1("myP");
}
document.getByElementId("myH1").onmouseout = function(){
    displayH1("myP");
}

function displayH1(myId){
    if(document.getByElementId(myId).style.display == "block"){
        document.getByElementId(myId).style.display = "none";
    }else{
        document.getByElementId(myId).style.display = "block";
    }

}

This invokes a function that toggles the paragraph's style when the mouse enters and leaves the element.这将调用一个函数,该函数在鼠标进入和离开元素时切换段落的样式。 It assumes that you have an id on both your h1 and p tags, in my example myH1 and myP respectively.它假设你的 h1 和 p 标签上都有一个 id,在我的例子中分别是 myH1 和 myP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM