DIV显示/隐藏代码问题?

时间:2012-08-11 18:34:40

标签: javascript html hide show

以下代码允许在用户滚动链接时显示div。问题是,当用户滚动链接时,div不会消失。无论如何我们可以做到这一点,当用户滚动链接时,div消失,但用户仍然可以将光标向下移动并与div中的项目进行交互.....任何帮助都将不胜感激。< / p>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type='text/javascript'> 
 /* <![CDATA[ */ 
document.getElementsByClassName = function(){ 
    if(arguments.length == 1) 
    arguments[1]='*'; 
  var retnode = []; 
  var myclass = new RegExp('\\b'+arguments[0]+'\\b'); 
  var elem = this.getElementsByTagName(arguments[1]); 
  for(var i = 0; i < elem.length; i++){ 
    var classes = elem[i].className; 
    if(myclass.test(classes)) 
      retnode.push(elem[i]); 
  }; 
  return retnode; 
}; 
window.onload=function(){ 
  var x = document.getElementsByClassName('HoverMe', 'a'); 
  for(var i = 0; i < x.length; i++){ 
    x[i].onmouseover=function(){ 
      var m = document.getElementsByClassName('HoverMe', 'a'); 
      var n = document.getElementsByClassName('showMe', 'div'); 
      for(var i = 0; i<m.length; i++){ 
        n[i].style.display = (m[i]==this)?'block':'none'; 
      }; 
    }; 
  }; 
  x = document.getElementsByClassName('showMe','div'); 
  for(var i = 0; i < x.length; i++){ 
    x[i].style.display = 'none'; 
  }; 
}; 
/* ]]> */ 
</script> 

</head>

<body>
<a class='HoverMe'>link 1</a><a class='HoverMe'>link 2</a>
<div class='showMe'>stuff 1</div><div class='showMe'>stuff 2</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

添加mouseout功能。在x[i].mouseover函数调用下添加以下代码:

x[i].onmouseout=function(){ 
  var m = document.getElementsByClassName('HoverMe', 'a'); 
  var n = document.getElementsByClassName('showMe', 'div'); 
  for(var i = 0; i<m.length; i++){ 
    n[i].style.display = 'none';
  }; 
};

在这里查看小提琴:http://jsfiddle.net/babcN/