是实现了目的,但是如果页面上有100个控件都要显示Div,那岂不是要在new Array后面写上100个id?

解决方案 »

  1.   

    <div id="fdu5eusfd">1个是不是</div>
    <div id="fdfi758ifd">1个是<div id="fgdfsfd">1个是不是</div>真不知道啊不是</div>
    <div id="shiybClick" onclick="hiddenOther(this);" style="cursor:pointer;" >请点击此处选择...<div>这里的内容也是噢!</div> </div >
    <div id="fdu5eusfd">1个是不是</div>
    <div id="fdfi758ifd">1个是<div id="fgdfsfd">1个是不是</div>真不知道啊不是</div>
    <div id="fdu5eusfd">1个是不是</div>
    <div id="fdfi758ifd">1个是<div id="fgdfsfd">1个是不是</div>真不知道啊不是</div>
    <div id="fdu5eusfd">1个是不是</div>
    <div id="fdfi758ifd">1个是<div id="fgdfsfd">1个是不是</div>真不知道啊不是</div>
    <p>这里是P标签</p>
    这里不应该隐藏的~!
    <script type="text/javascript">
    $ = function(d) {document.getElementById(d);}
    function hiddenOther(thisDiv) {
    var temp = thisDiv;
    while(temp.previousSibling) {
    if(temp.nextSibling.nodeName == 'DIV') temp.previousSibling.style.display="none";
    temp = temp.previousSibling;
    }
    var temp = thisDiv;
    while(temp.nextSibling) {
    if(temp.nextSibling.nodeName == 'DIV') temp.nextSibling.style.display="none";
    temp = temp.nextSibling;
    }
    }</script>你试试这种思路行不行~但不知道为什么我这个代码无法在Firefox下运行,
      

  2.   


    <html>
    <script>
    function show(){
        var shiyDiv=document.getElementById("shiybDiv");
        shiyDiv.style.display="";
        shiyDiv.iShow=true;
    }
    function hide(){
        var shiyDiv=document.getElementById("shiybDiv");
        if(!shiyDiv.iShow){
            document.getElementById("shiybDiv").style.display="none";
        }
        shiyDiv.iShow=false;
    }
    </script>
    <body onload="document.getElementById('shiybDiv').iShow=false;" onclick="hide()">
    <div id="shiybDiv" style="display:none;" onclick="show()" >aadasfsad </div > 
    <div id="shiybClick" onclick="show()" style="cursor:pointer;" >请点击此处选择... </div > 
    </body>
    </html>
      

  3.   

    我发现下面的方法更好,不用点击就可以自动弹出:<div id="shiybClick" onmouseover="document.getElementById('shiybDiv').style.display='';" 
      onmouseout="document.getElementById('shiybDiv').style.display='none';" 
      style="cursor:pointer;background-color:blue;width:300px;">请点击此处选择... </div> 
    <div id="shiybDiv" style="display:none;background-color:yellow;width:200px;" 
      onmouseover="document.getElementById('shiybDiv').style.display='';" 
      onmouseout="document.getElementById('shiybDiv').style.display='none';">
      aadasfsad <br>fdasf<br>dfdddd<br>ddddd</div> 
      

  4.   

    <!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=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <div id="shiybDiv" style="display:none;" >aadasfsad </div >
    <div id="shiybClick"  style="cursor:pointer;">请点击此处选择...</div >
    <div style="cursor:pointer;">check1</div >
    <div style="cursor:pointer;">check2</div >
    <div id="shiybClick1"  style="cursor:pointer;">请点击此处选择...</div >
    <div style="cursor:pointer;">check3</div >
    </body>
    </html>
    <script language="javascript">
    document.onclick = function()
    {
    //document点击时隐藏指定元素 因为document是冒泡的最后一层所以只要中途终止冒泡就不会触发这个事件
        document.getElementById('shiybDiv').style.display='none';

    window.onload = function()
    {
        var showSrc = ['shiybDiv','shiybClick','shiybClick1'];//允许点击的ID数组
    var fc = function(event)//事件方法
    {
    document.getElementById('shiybDiv').style.display="";//显示指定元素
    event.cancelBubble=true;//终止冒泡
    }
        for(var i=0,l=showSrc.length;i<l;i++)//遍历每个ID
    document.getElementById(showSrc[i]).onclick = fc;//给这个元素的onclick事件加上方法
    }
    </script>
      

  5.   

    用jquery,然后给要隐藏的div统一设置一个class属性值,点击页面其它地方的时候把class为这些值的div全部隐藏