从列表中显示不同的弹出窗口。

时间:2014-02-19 02:52:58

标签: javascript jquery css html

我在显示页面上有多个项目的弹出窗口时出现问题。从本质上讲,它是页面下方项目的垂直“列表”。到目前为止我有两个。当我单击第一组信息时,第一组信息会正确显示,但是当我单击第二项时,它会显示弹出窗口中的第一组信息。任何帮助表示赞赏,谢谢!

    <p> <a class="show-popup" href="#">Manual Therapy</a></p>
    <div class="overlay-bg">
     <div class="overlay-content">
      <h2>Manual Therapy</h2>
      <p>FIRST SET OF INFORMATION DISPLAYED HERE</p>
      <button class="close-btn">Close</button>
     </div>
    </div>
    <a class="show-popup" href="#">LIST ITEM 2</a>
     <div class="overlay-bg">
      <div class="overlay-content">
       <h2>Low Level LASER Therapy</h2>
       <p>SECOND SET OF INFORMATION DISPLAYED HERE</p>
       <button class="close-btn">Close</button>
     </div>
   </div>

这是CSS

.overlay-bg {
display: none;
position: fixed;
top: 0;
left: 0;
height:100%;
width: 100%;
cursor: pointer;
background: #000; /* fallback */
background: rgba(0,0,0,0.75);
}
.overlay-content {
background: #fff;
padding: 1%;
width: 700px;
height: 400px;
overflow:auto;
position: relative;
top: 15%;
left: 30%;
margin: 0 0 0 -10%; /* add negative left margin for half the width to center the div */
cursor: default;
border-radius: 4px;
box-shadow: 0 0 5px rgba(0,0,0,0.9);
}

这是JS

$(document).ready(function(){
// show popup when you click on the link
$('.show-popup').click(function(event){
event.preventDefault(); // disable normal link function so that it doesn't refresh the page
$('.overlay-bg').show(); //display your popup
});

// hide popup when user clicks on close button
$('.close-btn').click(function(){
$('.overlay-bg').hide(); // hide the overlay
});

// hides the popup if user clicks anywhere outside the container
$('.overlay-bg').click(function(){
    $('.overlay-bg').hide();
})
// prevents the overlay from closing if user clicks inside the popup overlay
$('.overlay-content').click(function(){
    return false;
});

});

3 个答案:

答案 0 :(得分:1)

您需要将第二个锚点包含在<p>标记内,然后您可以更改:

$('.overlay-bg').show();

为:

$(this).parent().next().show();

这可以帮助您根据点击的.overlay-bg

来定位.show-popup

<强> Fiddle Demo

答案 1 :(得分:1)

<强> LIVE DEMO

在锚点href内放置您想要查看的所需弹出内容的ID:

<强> CSS:

.overlay-content {
  display:none; /* NOTE THIS */

HTML: (仅使用一个POPUP元素,但更多内容元素!)

<a class="show-popup" href="#cont1">Manual Therapy</a>
<a class="show-popup" href="#cont2">LIST ITEM 2</a>


<div class="overlay-bg">
  <div id="cont1" class="overlay-content">
      <h2>Manual Therapy</h2>
      <p>FIRST SET OF INFORMATION DISPLAYED HERE</p>
      <button class="close-btn">Close</button>
  </div>
  <div id="cont2" class="overlay-content">
       <h2>Low Level LASER Therapy</h2>
       <p>SECOND SET OF INFORMATION DISPLAYED HERE</p>
       <button class="close-btn">Close</button>
  </div>
</div>

<强> JQ:

$(function(){

  $('.show-popup').click(function(event){
    event.preventDefault();
    $('.overlay-bg,'+ $(this).attr('href')).show(); // Show overlay, but also
  });                                               // the popup ID content
                                                    // taken from the anchor HREF
  $('.overlay-bg, .close-btn').click(function(){
      $('.overlay-bg, .overlay-content').hide();
  });

  $('.overlay-content').click(function(event){
      event.stopPropagation();
  });

});

这样做,您甚至可以在弹出只有一个CLOSE按钮内,但我会留给您, 我希望你能得到一般的想法......

另外:看看这个问题:event.preventDefault() vs. return false

答案 2 :(得分:0)

为链接,div和按钮添加一个你想要显示的内容。

<a class="show-popup" href="#" id="1">Manual Therapy</a></p>
    <div class="overlay-bg" id="div_1">
     <div class="overlay-content">
      <h2>Manual Therapy</h2>
      <p>FIRST SET OF INFORMATION DISPLAYED HERE</p>
      <button class="close-btn">Close</button>
     </div>
    </div>
    <a class="show-popup" href="#" id="2">LIST ITEM 2</a>
     <div class="overlay-bg" id="div_2">
      <div class="overlay-content">
       <h2>Low Level LASER Therapy</h2>
       <p>SECOND SET OF INFORMATION DISPLAYED HERE</p>
       <button class="close-btn">Close</button>
     </div>
   </div>

并使用$(this)和id。

<script>
$(document).ready(function(){
// show popup when you click on the link
$('.show-popup').click(function(event){
    var id = $(this).attr("id");
event.preventDefault(); // disable normal link function so that it doesn't refresh the page
$('#div_'+id).show(); //display your popup
});

// hide popup when user clicks on close button
$('.close-btn').click(function(){
    var id = $(this).attr("id");
$('#div_'+id).hide(); // hide the overlay
});

// hides the popup if user clicks anywhere outside the container
$('.overlay-bg').click(function(){
    $(this).hide();
})
// prevents the overlay from closing if user clicks inside the popup overlay
$('.overlay-content').click(function(){
    return false;
});

});
</script>