在鼠标悬停 div 上弹出消息

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

试图让一个简单的弹出窗口出现在

mouseover
a
div
我按照答案 Description Box 使用“onmouseover” 但它不起作用。我错过了什么?

<!DOCTYPE html>
<head>
<style>
    .parent .popup {
      display: none;
    }
    .parent:hover .popup {
      display: block;
    }
</style>
</head>

<body>
<script type="text/javascript">
    var e = document.getElementById('#parent');
    e.onmouseover = function() {
      document.getElementById('popup').style.display = 'block';
    }
    e.onmouseout = function() {
      document.getElementById('popup').style.display = 'none';
    }
</script>


<div id="parent">
    This is the main container.
    <div id="popup" style="display: none">some text here</div>
</div>

</body> 
</html>
javascript html mouseover
3个回答
4
投票

有几个问题。您在 CSS 中引用类(. 是类,# 是 id)其次,您不需要重载 CSS display none 样式。最后,在这种情况下你不需要 JavaScript。

查看工作示例。

<!DOCTYPE html>
<head>
<style>
    #parent #popup {
      display: none;
    }
    #parent:hover #popup {
      display: block;
    }
</style>
</head>

<body>


<div id="parent">
    This is the main container.
    <div id="popup">some text here</div>
</div>

</body> 
</html>


0
投票

如果弹出窗口在外部(不在父级内部),则:

#popup {
  display: none;
}

#sibling:hover+#popup {
  display: block;
}
<div id="sibling">
  This is the main container.
</div>
<div id="popup">some text here</div>


0
投票

只需在 html 标签内添加

title="Some popup text"
例如...

<button class="toolbar-item-fontcolour" 
        title="Font colour"  
        id="btnFontColour" 
        @onclick="ShowColourSelectorFont">A</button> 
© www.soinside.com 2019 - 2024. All rights reserved.