我用js做了个弹出的div,现在想 如果不是点击在本div上,就关闭这个div 怎么写?

解决方案 »

  1.   

    document.onclick=function(){close div}
      

  2.   

    上面的没看清楚吧 是不是点在本div上
      

  3.   

    1楼可以的吧,在里面再判断如果没有再点中DIV,就关闭DIV,如何判断:通过一个变量,点第一次时为1,再点为0,所以判断在点document时先判断这个变里是否为1,是刚关闭,否则不关
      

  4.   

    1、弹出的div下有遮罩层,在遮罩层上添加个onclick事件关闭打开的div
    2、没有遮罩层的话,直接在body上添加onclick事件关闭即可。
      

  5.   

    <html>
    <head>
    <script type='text/javascript'>
    document.onclick = function(e) {
    var el = e ? e.target : event.srcElement;
    if (!!el['arg'] && el['arg'] == 'div') {
    alert('点在div上了');
    } else {
    alert('没点在div上');
    }
    }
    </script>
    </head>
    <body>
    <div style='width:300px;height:300px;border:1px solid black;' arg='div'></div>
    </body>
    </html>
      

  6.   

    div.onclick=function(){
    //阻止事件冒泡
    }
      

  7.   

    那怎样判断没点在这个div上呢
      

  8.   

    给div本身再注册一个事件,让它什么都不做,然后阻止冒泡不就得了
      

  9.   

    6楼的在ff下运行不了 2个判断都是没点在div上
      

  10.   

    WebAdvocate  帮帮忙写个哈 js我很差的
      

  11.   

    <html>
    <head>
        <script type='text/javascript'>
            window.onload = function() {
                var info = document.getElementById("info");
                document.onclick = function() {
                    alert("U has been clicked the document, info will be closed.");
                    info.style.display = "none";
                }
                info.onclick = function(e) {
                    alert("div has been clicked, nothing will be done!");
                    if (e && e.stopPropagation) {
                        e.stopPropagation();
                    } else {
                        window.event.cancelBubble = true;
                    }
                }
            }    </script>
    </head>
    <body>
    <div style="width:300px; height:300px; border:1px solid red;" id="info">This is div info</div>
    </body>
    </html>
      

  12.   


    <html>
    <head>
    <script type='text/javascript'>
    document.onclick = function(e) {
    var el = e ? e.target : event.srcElement;
    if (!!el['arg'] && el['arg'] == 'div') {
    alert('点在div上了');
    } else if (!!el.attributes['arg'] && el.attributes['arg'].value == 'div'){
    alert('点在div上了');
    } else {
    alert('没点在div上');
    }
    }
    </script>
    </head>
    <body>
    <div style='width:300px;height:300px;border:1px solid black;' arg='div'></div>
    </body>
    </html>刚刚改了一下,现在在IE和FF下都好使