鱼C论坛

 找回密码
 立即注册
查看: 3335|回复: 2

[已解决]如何使用js实现点击空白地方消失弹窗

[复制链接]
发表于 2019-6-16 09:54:10 | 显示全部楼层 |阅读模式
3鱼币
我的要求是:点击按钮弹出表单,此时不能再点击任何按钮只能操作表单,但是可以点击空白地方让表单消失
目前进展:我用了两个div,一个div用于弹出表单,另一个div进行覆盖让鼠标不能点击其他地方
问题:不知道怎么实现点击表单之外的地方让这两个div消失从而可以再次点击其他功能按钮
  1. jsp代码:
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5.   <head>
  6.     <title>My JSP 'MyJsp.jsp' starting page</title>
  7.         <link rel="stylesheet" type="text/css" href="css/test.css">
  8.          <script type="text/javascript" src="js/left_menu.js"></script>
  9.   </head>
  10.   <body>
  11.   <input type="button" onclick="borrowbook()" id="leftmenu1" value="借阅图书"><br>     
  12.   <input type="button" onclick="borrowbook()" id="leftmenu1" value="归还图书"><br>     
  13.    <div id="borrowbook">
  14.             <div style="width:500px;height:40px;">
  15.                 <hr>
  16.                 <form action="operbook.do" style="margin-left: 50px;" method="post" onsubmit="return operbookTest();">
  17.                           <input type="hidden" name="oper" value="借阅">
  18.                             请输入所借阅书籍的编号<input type="text" name="bid" id="operbid"><br>
  19.                             请输入所借阅书籍的书名<input type="text" name="bname" id="operbname"><br>
  20.                             请输入你想要借阅的本数<input type="text" name="bcount" id="operbcount"><br>
  21.                             <input type="submit" value="确定">       
  22.                             <input type="reset" value="重填">       
  23.                 </form>
  24.             </div>
  25.         </div>
  26.         <div id="borrowbook_light">
  27.         </div>
  28.   </body>
  29. </html>
  30. js代码:
  31. function borrowbook() {
  32.     var t1 = document.getElementById('borrowbook_light');
  33.     var t2 = document.getElementById('borrowbook');
  34.     t1.style.display = 'block';
  35.     t2.style.display = 'block';
  36. }
  37. css代码:
  38. @CHARSET "UTF-8";
  39. #borrowbook_light{ /*整个弹窗的页面*/
  40.     opacity: 0.8; /*透明度*/
  41.     width: 100%; /*宽度*/
  42.     height: 100%; /*高度,不能百分百*/
  43.     background: #000; /*背景色*/
  44.     position: absolute;
  45.     top: 0;
  46.     left: 0; /*定位*/
  47.     display: none; /*隐藏*/
  48.     z-index: 12; /*覆盖*/
  49. }
  50. #borrowbook{ /* 弹框的页面*/
  51.         width: 500px; /*宽度*/
  52.         height: 500px; /*高度*/
  53.         background: #fff; /*背景色*/
  54.         display: none; /*隐藏*/
  55.         z-index: 13; /*覆盖*/
  56.         position: absolute;
  57.         top: 100px;
  58.         left: 34%; /*定位*/
  59. }
复制代码
最佳答案
2019-6-16 09:54:11
  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <title>My JSP 'MyJsp.jsp' starting page</title>
  5.     <!-- <link rel="stylesheet" type="text/css" href="css/test.css">
  6.     <script type="text/javascript" src="js/left_menu.js"></script> -->
  7.     <style>
  8.         #borrowbook_light {
  9.             /*整个弹窗的页面*/
  10.             opacity: 0.8;
  11.             /*透明度*/
  12.             width: 100%;
  13.             /*宽度*/
  14.             height: 100%;
  15.             /*高度,不能百分百*/
  16.             background: #000;
  17.             /*背景色*/
  18.             position: absolute;
  19.             top: 0;
  20.             left: 0;
  21.             /*定位*/
  22.             display: none;
  23.             /*隐藏*/
  24.             z-index: 12;
  25.             /*覆盖*/
  26.         }
  27.         
  28.         #borrowbook {
  29.             /* 弹框的页面*/
  30.             width: 500px;
  31.             /*宽度*/
  32.             height: 500px;
  33.             /*高度*/
  34.             background: #fff;
  35.             /*背景色*/
  36.             display: none;
  37.             /*隐藏*/
  38.             z-index: 13;
  39.             /*覆盖*/
  40.             position: absolute;
  41.             top: 100px;
  42.             left: 34%;
  43.             /*定位*/
  44.         }
  45.     </style>
  46. </head>

  47. <body>
  48.     <input type="button" onclick="borrowbook()" id="leftmenu1" value="借阅图书"><br>
  49.     <input type="button" onclick="borrowbook()" id="leftmenu1" value="归还图书"><br>
  50.     <div id="borrowbook">
  51.         <div style="width:500px;height:40px;">
  52.             <hr>
  53.             <form action="operbook.do" style="margin-left: 50px;" method="post" onsubmit="return operbookTest();">
  54.                 <input type="hidden" name="oper" value="借阅"> 请输入所借阅书籍的编号
  55.                 <input type="text" name="bid" id="operbid"><br> 请输入所借阅书籍的书名
  56.                 <input type="text" name="bname" id="operbname"><br> 请输入你想要借阅的本数
  57.                 <input type="text" name="bcount" id="operbcount"><br>
  58.                 <input type="submit" value="确定">
  59.                 <input type="reset" value="重填">
  60.             </form>
  61.         </div>
  62.     </div>
  63.     <div id="borrowbook_light" onclick="hiddenLight()">
  64.     </div>
  65.     <script>
  66.         var t1 = document.getElementById('borrowbook_light');
  67.         var t2 = document.getElementById('borrowbook');

  68.         function borrowbook() {
  69.             t1.style.display = 'block';
  70.             t2.style.display = 'block';
  71.         }

  72.         function hiddenLight() {
  73.             t1.style.display = 'none';
  74.             t2.style.display = 'none';
  75.         }
  76.     </script>
  77. </body>

  78. </html>
复制代码



是这个意思吗?

点击按钮之后

点击按钮之后

点击按钮之前

点击按钮之前

最佳答案

查看完整内容

是这个意思吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-16 09:54:11 | 显示全部楼层    本楼为最佳答案   
  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <title>My JSP 'MyJsp.jsp' starting page</title>
  5.     <!-- <link rel="stylesheet" type="text/css" href="css/test.css">
  6.     <script type="text/javascript" src="js/left_menu.js"></script> -->
  7.     <style>
  8.         #borrowbook_light {
  9.             /*整个弹窗的页面*/
  10.             opacity: 0.8;
  11.             /*透明度*/
  12.             width: 100%;
  13.             /*宽度*/
  14.             height: 100%;
  15.             /*高度,不能百分百*/
  16.             background: #000;
  17.             /*背景色*/
  18.             position: absolute;
  19.             top: 0;
  20.             left: 0;
  21.             /*定位*/
  22.             display: none;
  23.             /*隐藏*/
  24.             z-index: 12;
  25.             /*覆盖*/
  26.         }
  27.         
  28.         #borrowbook {
  29.             /* 弹框的页面*/
  30.             width: 500px;
  31.             /*宽度*/
  32.             height: 500px;
  33.             /*高度*/
  34.             background: #fff;
  35.             /*背景色*/
  36.             display: none;
  37.             /*隐藏*/
  38.             z-index: 13;
  39.             /*覆盖*/
  40.             position: absolute;
  41.             top: 100px;
  42.             left: 34%;
  43.             /*定位*/
  44.         }
  45.     </style>
  46. </head>

  47. <body>
  48.     <input type="button" onclick="borrowbook()" id="leftmenu1" value="借阅图书"><br>
  49.     <input type="button" onclick="borrowbook()" id="leftmenu1" value="归还图书"><br>
  50.     <div id="borrowbook">
  51.         <div style="width:500px;height:40px;">
  52.             <hr>
  53.             <form action="operbook.do" style="margin-left: 50px;" method="post" onsubmit="return operbookTest();">
  54.                 <input type="hidden" name="oper" value="借阅"> 请输入所借阅书籍的编号
  55.                 <input type="text" name="bid" id="operbid"><br> 请输入所借阅书籍的书名
  56.                 <input type="text" name="bname" id="operbname"><br> 请输入你想要借阅的本数
  57.                 <input type="text" name="bcount" id="operbcount"><br>
  58.                 <input type="submit" value="确定">
  59.                 <input type="reset" value="重填">
  60.             </form>
  61.         </div>
  62.     </div>
  63.     <div id="borrowbook_light" onclick="hiddenLight()">
  64.     </div>
  65.     <script>
  66.         var t1 = document.getElementById('borrowbook_light');
  67.         var t2 = document.getElementById('borrowbook');

  68.         function borrowbook() {
  69.             t1.style.display = 'block';
  70.             t2.style.display = 'block';
  71.         }

  72.         function hiddenLight() {
  73.             t1.style.display = 'none';
  74.             t2.style.display = 'none';
  75.         }
  76.     </script>
  77. </body>

  78. </html>
复制代码



是这个意思吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-18 22:46:43 | 显示全部楼层
你这里想实现类似bootstr4的modal组件的功能??
最简单方案就是套入bootstrap4

如果不套用,想自己写的话,可以尝试这样
把黑色部分设置一个方法。  
当有点击事件的话,就把表单显示隐藏。  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 12:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表