Jquery 在窗口中创建多个弹出窗口

Jquery 在窗口中创建多个弹出窗口,jquery,html,css,Jquery,Html,Css,我对jQuery还是相当陌生的,我正试图弄清楚如何在一个窗口中创建多个弹出窗口。我希望每个文本打开一个不同的弹出窗口,包含不同的内容。当我在弹出窗口外单击时,我希望弹出窗口关闭,而不是只在按下Esc键时关闭。非常感谢您的帮助。提前谢谢 HTML: <div id="main"> <h1 class="button" id="applepie">Apple Pie</h1> <div clas

我对jQuery还是相当陌生的,我正试图弄清楚如何在一个窗口中创建多个弹出窗口。我希望每个文本打开一个不同的弹出窗口,包含不同的内容。当我在弹出窗口外单击时,我希望弹出窗口关闭,而不是只在按下Esc键时关闭。非常感谢您的帮助。提前谢谢

HTML:

     <div id="main">
           <h1 class="button" id="applepie">Apple Pie</h1>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>

                <h3>Asian Noodles</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>


                <h3>Avocado Roll</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>


                <h3>Bruscetta</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">bye</div>


                <h3>Bagels</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">eat</div>


                <h3>Banana Pudding</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">hungey</div>


     </div>
.modal-mask{
    width:100%;
    height:100%;
    position: absolute;
    top:0px;
    z-index: 100;
    background-color:#000;
    opacity:0.4;
    display:none;
    }



.modal-popup{
     position:fixed;
     top: 50%;
     left: 50%;
     width: 5%;
     height: 5%;
     z-index: 101;
     background-color:#fff; 
     display:none;
     }
$(function(){
  $(".button").on("click", function(){

    $(".modal-mask").css("display", "block");
    $(".modal-popup").css("display", "block");


  $(document).on("keydown", function(event){
   if(event.keyCode === 27){
    $(".modal-mask").css("display", "");
       $(".modal-popup").css({
           "display": "",
           "width": "",
           "height": "",
           "top": "",
           "left": ""
       });
    }
  });
});
JS:

     <div id="main">
           <h1 class="button" id="applepie">Apple Pie</h1>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>

                <h3>Asian Noodles</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>


                <h3>Avocado Roll</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>


                <h3>Bruscetta</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">bye</div>


                <h3>Bagels</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">eat</div>


                <h3>Banana Pudding</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup">hungey</div>


     </div>
.modal-mask{
    width:100%;
    height:100%;
    position: absolute;
    top:0px;
    z-index: 100;
    background-color:#000;
    opacity:0.4;
    display:none;
    }



.modal-popup{
     position:fixed;
     top: 50%;
     left: 50%;
     width: 5%;
     height: 5%;
     z-index: 101;
     background-color:#fff; 
     display:none;
     }
$(function(){
  $(".button").on("click", function(){

    $(".modal-mask").css("display", "block");
    $(".modal-popup").css("display", "block");


  $(document).on("keydown", function(event){
   if(event.keyCode === 27){
    $(".modal-mask").css("display", "");
       $(".modal-popup").css({
           "display": "",
           "width": "",
           "height": "",
           "top": "",
           "left": ""
       });
    }
  });
});

通过侦听文档捕获所有单击事件,但 使用事件委托表示法,以便我们可以计算 发出单击的最低级别元素。像这样:

$(document).on('click', '*', function(){
    //alternatively, you can use the 'keydown' event, as you have.

    //if the click did *not* bubble up through the modal, 
    //then the click was outside it, on something else:
    if(!$(this).parents().hasClass('modal-popup'){
        //do what you need to do when user clicks outside the modal
        //i.e. close the modal
    }
})

通过侦听文档捕获所有单击事件,但 使用事件委托表示法,以便我们可以计算 发出单击的最低级别元素。像这样:

$(document).on('click', '*', function(){
    //alternatively, you can use the 'keydown' event, as you have.

    //if the click did *not* bubble up through the modal, 
    //then the click was outside it, on something else:
    if(!$(this).parents().hasClass('modal-popup'){
        //do what you need to do when user clicks outside the modal
        //i.e. close the modal
    }
})

通过侦听文档捕获所有单击事件,但 使用事件委托表示法,以便我们可以计算 发出单击的最低级别元素。像这样:

$(document).on('click', '*', function(){
    //alternatively, you can use the 'keydown' event, as you have.

    //if the click did *not* bubble up through the modal, 
    //then the click was outside it, on something else:
    if(!$(this).parents().hasClass('modal-popup'){
        //do what you need to do when user clicks outside the modal
        //i.e. close the modal
    }
})

通过侦听文档捕获所有单击事件,但 使用事件委托表示法,以便我们可以计算 发出单击的最低级别元素。像这样:

$(document).on('click', '*', function(){
    //alternatively, you can use the 'keydown' event, as you have.

    //if the click did *not* bubble up through the modal, 
    //then the click was outside it, on something else:
    if(!$(this).parents().hasClass('modal-popup'){
        //do what you need to do when user clicks outside the modal
        //i.e. close the modal
    }
})

解决了的
  • 首先对代码进行更改,将每个头和2个模态div包装在内容包装器div中
  • 单击标题时,会检测到父对象,并设置第二个和第三个元素
    显示:block
    ,反之亦然
HTML CSS 输出

解决了的
  • 首先对代码进行更改,将每个头和2个模态div包装在内容包装器div中
  • 单击标题时,会检测到父对象,并设置第二个和第三个元素
    显示:block
    ,反之亦然
HTML CSS 输出

解决了的
  • 首先对代码进行更改,将每个头和2个模态div包装在内容包装器div中
  • 单击标题时,会检测到父对象,并设置第二个和第三个元素
    显示:block
    ,反之亦然
HTML CSS 输出

解决了的
  • 首先对代码进行更改,将每个头和2个模态div包装在内容包装器div中
  • 单击标题时,会检测到父对象,并设置第二个和第三个元素
    显示:block
    ,反之亦然
HTML CSS 输出

只是一个建议,在其中使用一个弹出窗口添加所有要显示的关于选定或单击对象的内容在模式中显示相应的内容,并在模式中隐藏其余内容您有什么问题?@user3127499好的,我会试试。但目前,即使我只有一个弹出模式,它也不起作用。知道为什么吗?@cale_b问题是:目前我的弹出窗口不工作。也,我希望每个文本的每个弹出窗口都包含不同的信息只是一个建议在其中使用一个弹出窗口添加所有要显示的内容关于所选或单击的对象在模式中显示相应的内容并在模式中隐藏其余内容有什么问题吗?@user3127499 ok,我试试看。但目前,即使我只有一个弹出模式,它也不起作用。知道为什么吗?@cale_b问题是:目前我的弹出窗口不工作。也,我希望每个文本的每个弹出窗口都包含不同的信息只是一个建议在其中使用一个弹出窗口添加所有要显示的内容关于所选或单击的对象在模式中显示相应的内容并在模式中隐藏其余内容有什么问题吗?@user3127499 ok,我试试看。但目前,即使我只有一个弹出模式,它也不起作用。知道为什么吗?@cale_b问题是:目前我的弹出窗口不工作。也,我希望每个文本的每个弹出窗口都包含不同的信息只是一个建议在其中使用一个弹出窗口添加所有要显示的内容关于所选或单击的对象在模式中显示相应的内容并在模式中隐藏其余内容有什么问题吗?@user3127499 ok,我试试看。但目前,即使我只有一个弹出模式,它也不起作用。知道为什么吗?@cale_b问题是:目前我的弹出窗口不工作。此外,我希望每个文本的弹出窗口包含不同的信息。我认为这是可行的。不过,我还有一个问题:有没有办法让我可以通过点击模式外部(在遮罩区域)来关闭弹出窗口,而不是按esc键?酷。我认为这是可行的。不过,我还有一个问题:有没有办法让我可以通过点击模式外部(在遮罩区域)来关闭弹出窗口,而不是按esc键?酷。我认为这是可行的。不过,我还有一个问题:有没有办法让我可以通过点击模式外部(在遮罩区域)来关闭弹出窗口,而不是按esc键?酷。我认为这是可行的。我还有另一个问题:有没有办法让我可以通过点击模式外部(在遮罩区域)而不是按esc键来关闭弹出窗口?