Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用鼠标单击外部边界关闭弹出框_Javascript_Html - Fatal编程技术网

Javascript 用鼠标单击外部边界关闭弹出框

Javascript 用鼠标单击外部边界关闭弹出框,javascript,html,Javascript,Html,我制作了这个主页。具有带有登录字段的弹出框。当出现弹出窗口时,背景变暗。但是,如果用户单击框边界以外的任何位置,则弹出窗口应关闭。我已经创建了closeOverlay函数,当用户在键盘上点击esc时,它就可以工作,但是当用户在框的边界之外点击时,什么都不会发生。当用户点击escape或将鼠标移出框边界并单击时,如何使其在这两种情况下都能工作 这是HTML代码 <head> <link rel="stylesheet" type="text/css" href="

我制作了这个主页。具有带有登录字段的弹出框。当出现弹出窗口时,背景变暗。但是,如果用户单击框边界以外的任何位置,则弹出窗口应关闭。我已经创建了closeOverlay函数,当用户在键盘上点击esc时,它就可以工作,但是当用户在框的边界之外点击时,什么都不会发生。当用户点击escape或将鼠标移出框边界并单击时,如何使其在这两种情况下都能工作

这是HTML代码

<head>
        <link rel="stylesheet" type="text/css" href="LAb2css.css">
</head>
<script>
    window.onload = function() {


        setInterval(move, 10000);

    }



    function move() { //makes the box move in relation to the box div
              var d = document.getElementById('moveQuestion');
              var boxh = document.getElementById('boxDIV').clientHeight - 100; //keeps the image in the box by subtracting height
              var boxw = document.getElementById('boxDIV').clientWidth - 100; //keeps the image in the box by subtracting the width

              document.onmousemove = function calc(e) {
                var x = e.clientX ;
                var y = e.clientY;

                if (x < boxw) {
                    d.style.left = x +'px';
                }

                if (y < boxh) {
                    d.style.top = y +'px';
                }

              };
          };


function signin() { //displays the sign in form
            var lg = document.getElementById('login');
            var ov = document.getElementById('BackDIV');
            ov.style.display = 'block';
            lg.style.display = 'block';
          };

function join() { //calls the join form for users to register

            var lg = document.getElementById('signup');
            var ov = document.getElementById('BackDIV');
            ov.style.display = 'block';
            lg.style.display = 'block';
            activeForm = 1; //sets the active form counter to 1; a form is active
        };
function recoverPass() { //displays the recoverPassword form for the user to recover password

            var ov = document.getElementById('BackDIV');
            ov.style.display = 'block';

            var rp = document.getElementById('recoverPassword');
            rp.style.display = 'block';
             //sets the active form counter to 1; a form is active
        };    





         function closeOverlay() { // function closes the black transparent overlay div by setting the display properties of all the forms to none 
            var ov = document.getElementById('BackDIV');
            var rp = document.getElementById('recoverPassword');
            var lg = document.getElementById('login');
            var sg = document.getElementById('signup');

            ov.style.display = 'none';
            rp.style.display = 'none';
            lg.style.display = 'none';
            sg.style.display = 'none';
          };




     function closeOverlay() { // function closes the black transparent overlay div by setting the display properties of all the forms to none 
            var ov = document.getElementById('BackDIV');
            var rp = document.getElementById('recoverPassword');
            var lg = document.getElementById('login');
            var sg = document.getElementById('signup');

            ov.style.display = 'none';
            rp.style.display = 'none';
            lg.style.display = 'none';
            sg.style.display = 'none';
          };



             function closeOverlay() { // function closes the black transparent overlay div by setting the display properties of all the forms to none 
            var ov = document.getElementById('BackDIV');
            var rp = document.getElementById('recoverPassword');
            var lg = document.getElementById('login');
            var sg = document.getElementById('signup');

            ov.style.display = 'none';
            rp.style.display = 'none';
            lg.style.display = 'none';
            sg.style.display = 'none';
          };


          document.onkeydown = function(evt) { //makes the escape key to call the closeOverlay() function
            evt = evt || window.event;
            if (evt.keyCode == 27) {
                closeOverlay();
            }
          };



</script>

<body onload = move()>

    <div id="BackDIV">
    </div>

    <p>
        <?php
            // Just for displaying the display type for testing

            echo 'Display type = ' . $display_type . '<br><br>';

            if ($display_type == 'Start') {
                echo 'StartPage<br>';
            }
            else if ($display_type == 'SignIn') {
                echo "StartPage with 'SignIn' popup box<br>"; 
            }
            else if ($display_type == 'Join') {
                echo "StartPage with 'Join' popup box<br>"; 
            }
            else if ($display_type == 'ForgotPassword') {
                echo "StartPage with 'ForgotPassword' popup box<br>"; 
            }
            else {
                echo 'StartPage with no popup box<br>'; 
            }
        ?>
    </p>


    <div id = "main">

        <div id = "dateDIV"><?php $t = time(); echo (date("Y-m-d H:i:s", $t)); ?></div>

        <div id = "titleDIV">
          <h2>Thompson Rivers University - Question/Answers</h2>                    
        </div>

        <div id ="dopDownDIV">
            <ul>
              <li>
                <a href = "#"><img src="images/responsive_menu.png" title="MENU" alt="Menu" id="MenuIMG" height="50PX" width="50px" /></a>
                <ul>
                  <li><a href="#" onclick = "signin()">SIGN IN</a></li>

                    <li><a href="#" onclick="join()">JOIN</a></li>

                    <li><a href="#" onclick="recoverPass()">FORGOT PASSWORD</a></li>
                </ul>
              </li>
            </ul>       
        </div>

        <div id="boxDIV"> <a href ="#"><img src ="images/question-mark.png" id="moveQuestion" title= "Image moving In Box" alt="ImgInbox" height = "100px" width = "100px"</a></div>

        <div id= "BottomDIV">
            <a href = "http://cs.tru.ca" target="_blank"> About Us </a>
        </div>

    </div>

<!--Div for Signup form on click -->
 <div id="signup" onclick="closeOverlay()">
            <form action="main.html" method="get" autocomplete="on">
                <input type="text" name="username" autofocus><br /><p>Your email address</p>
                <input type="password" name="password"><br /><p>Password</p>
                <br /><br /><br />
                <input id="formButton" type="submit" value="Join">
                <a href="StartPage.html">
                    <input type="button" value="Cancel"/>
                </a>
            </form>
 </div>


<!--DIv for recoverPassword -->

<div id="recoverPassword" onclick="closeOverlay()">
            <!--<h1>Recover Password</h1> -->
            <form action="main.html" method="get" autocomplete="on">
                <input type="text" name="recoveremail" placeholder="The email address you registered with" autofocus><br /><p>Username (or email address)</p>

                <input id="formButton" type="submit">
                <a href="StartPage.html">
                    <input type="button" value="Cancel"/>
                </a>
            </form>
        </div>  

<!--Div for Login -->
    <div id="login" onclick="closeOverlay()">

            <form action="controller.php" method="post" autocomplete="on" >
                <input type='hidden' name='command' value='SignIn'>
                <input type="text" name="user" autofocus value="<?php echo $username ?>"> <?php echo $error_msg_username; ?> ><br />                            <p>Username (or email address)</p>
                <input type="password" name="pass" value="<?php echo $password ?>"> <?php echo $error_msg_password ?> ><br /><p>Password</p>
                <br/>
                <input type="submit" id="formButton" value="Sign In" >
                <a href="StartPage.html">
                    <input type="button" value="Cancel"/>
                </a>


            </form>
        </div>





</body>

</html>
试试这个

<div id="BackDIV" onclick="closeOverlay();"></div>

像这样?将事件侦听器添加到窗口,如果需要,请删除“确认”

var pop=document.getElementById'over';window.addEventListener'click'函数{ 确认“关闭弹出窗口”? pop.style.display=none:pop.style.display=block; }; 超过{最小宽度:80%;最小高度:100px;背景色:红色;}
这个问题与jQuery有什么关系?看起来你不是在用它。为什么有多个“closeOverlay”函数?为了回答你的问题:你能不能在BackDIV中添加一个eventListener?我将你的代码添加到HTML的顶部脚本部分,但它似乎没有任何作用:我不明白为什么这不起作用,你能在codepen或JSFIDLE中设置你的示例,这样我就可以看到它起作用了吗?当你单击下拉列表中的任何链接时,它仅通过esc退出,而不是用鼠标单击外部。这是JSFIDLE链接:很抱歉,图像不工作。当您浏览左上角的菜单时,单击3个链接中的任意一个。只有当我们在键盘上按esc键时,弹出窗口才会关闭,而不是在弹出区域外单击。编辑了我的答案,只是确认这在您的JSFIDLE中起作用。试一试,假设红色区域是我添加代码的弹出窗口,除了确认部分。但是在添加它之后,当我单击它们的链接时,我的弹出窗口一点也不显示我的坏消息,在您单击窗口关闭它们的那一刻,将其更改为window.addEventListener'click',function{ifdocument.getElementById'BackDIV'{closeOverlay;};如果他们开着,它就会关上。