用于mouseOver div的其他方式

时间:2014-09-08 09:11:33

标签: javascript html

我在屏幕中间做了一个div,当指针进入div时,屏幕上会出现一个警告。还有其他方法吗?(不是mouseOver,鼠标输入和悬停的东西)

<body> 
<script>
function myBox(){
document.addEventListener('mouseover',function msg(){  alert("You r in the box")  })
}
</script>


<div onMouseOver="myBox()" id="box"> </div>  
</body>

顺便说一下,我的代码是正常的,我需要用其他方式来完成而不是mouseOver和悬停...

2 个答案:

答案 0 :(得分:1)

您可以使用css :悬停显示:无;
但是css无法制作警报框架,所以它只能改变某个元素的样式

答案 1 :(得分:1)

  

仅限CSS   的 HTML

<a>Hover over me!</a>
<div>Stuff shown on hover</div>

<强> CSS

div {
    display: none;
}

a:hover + div {
    display: block;
}

DEMO

  

<强>的Javascript

function isOutside(evt, parent) {
  var elem = evt.relatedTarget || evt.toElement || evt.fromElement

  while ( elem && elem !== parent) {
    elem = elem.parentNode;
  }

  if ( elem !== parent) {
    return true
  }
}

var parent = document.getElementById('parent')

parent.onmouseover = parent.onmouseout = function(e) {
  e = e || event

  if (isOutside(e, this)) {
    parent.nextSibling.nodeValue = new Date() + ' ' + e.type 
  } 
}

<强> HTML

<div style="padding:10px;border: 1px solid blue" id="parent">
 <p>Move the mouse in and outside of here. The blue box parent has <i>many</i> other <b>elements</b> inside.</p>
 <p>They do not generate extra `mouseover/mouseout` events.</p>
</div>
event

DEMO