如何使用jquery .load打开模态

时间:2017-03-10 00:57:42

标签: javascript jquery html css

您好我编写了一个代码,以便在单击图像后打开模式。例如,如果我点击pi书籍封面的生活,它将打开一个模式显示有关它的信息。但由于某种原因,它不起作用,我不知道为什么。另请注意,pi的生命只有一个唯一的ID,因为我首先只在一个图像上测试它。代码在

之下

enter image description here

$(document).ready(function() {
  var $modal = $("#myModal");
  $("#lifeofpi").click(function() {
    $modal.show();
  });
  $modal.find('.close').click(function() {
    $modal.hide();
  });
});
/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.review-img {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class='images'>
  <img class="review-img" id="lifeofpi" src="./images/lifeofpi.jpg"></img>
  <img class="review-img" src="./images/kiterunner.jpg"></img>
  <img class="review-img" src="./images/starwars.jpg"></img>
  <img class="review-img" src="./images/twilight.jpg"></img>
</section>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

2 个答案:

答案 0 :(得分:2)

您需要使用display: none更改css属性show(),以使模式可见。

$(function(){
  var $modal = $("#myModal");
  $(".review-img").each(function() {
    var $image= $(this);
    $image.on("click", function() {
      var url = '/bookDetails/' + $image.data("bookid") + '.html';
      // Test:
      url = 'https://httpbin.org/html';
      $modal.find('.modal-content p').html('Loading...').load(url);
      $modal.show();
    });

  });
  $modal.find('.close').click(function() {
    $modal.hide();
  });
});
/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.review-img {
  cursor: pointer;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class='images'>
  <img class="review-img" data-bookid="lifeOfPi" src="https://images-na.ssl-images-amazon.com/images/I/51atapp7YTL._AC_US320_QL65_.jpg" alt="Life of Pi"></img>
  <img class="review-img" data-bookid="kiteRunner" src="https://images-na.ssl-images-amazon.com/images/I/51MtGFNeYjL._AC_US320_QL65_.jpg" alt="Kite Runner"></img>
  <img class="review-img" data-bookid="starWars" src="https://images-na.ssl-images-amazon.com/images/I/51oqkfvEwZL._AC_US320_QL65_.jpg" alt="Star Wars"></img>
  <img class="review-img" data-bookid="twilight" src="https://images-na.ssl-images-amazon.com/images/I/41K99+cInvL._AC_US320_QL65_.jpg" alt="Twilight"></img>
</section>

<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Book details loading...</p>
  </div>
</div>

答案 1 :(得分:2)

我认为你需要一种动态的方式来做到这一点。这是我的解决方案

注意:我编辑过因为图片太大了。

$(document).ready(function() {
  var $modal = $("#myModal");
  $(".review-img").each(function() {
    var element = $(this);
    element.on("click", function() {

      var alt = element.attr("alt")
      console.log(alt);
      $modal.find('p').html('Some text in the Modal.. ' + alt);
      $modal.show();
    });

    $modal.find('.close').click(function() {
      $modal.hide();
    });
  });
});
/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.review-img {
  cursor: pointer;
  height: 160px; // height
  //width: 30%; // width
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class='images'>
  <img class="review-img" src="http://i.imgur.com/t8SXpzI.jpg" alt="lifeofpi"></img>
  <img class="review-img" src="http://i.imgur.com/FqYNsbY.jpg" alt="kiterunner"></img>
  <img class="review-img" src="http://i.imgur.com/NCkqUVz.jpg" alt="starwars"></img>
  <img class="review-img" src="http://i.imgur.com/fDvqMaZ.jpg" alt="twilight"></img>
</section>

<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>
</div>

相关问题