繁体   English   中英

如何使用 Javascript 创建具有动态信息的模态?

[英]How do I create a modal with dynamic information using Javascript?

我知道这很简单,但我已经很累了,我似乎无法找到正确的关键字来在搜索引擎上找到解决方案......我正在构建一个简单的前端电子商务网站,我我已经设置了模态,但我需要它根据我单击的产品显示正确的信息......我该怎么做?

编辑:我没有输入代码的坏处......

    <html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="modal.css">
    <title>Merlin's Potions</title>
  </head>
<h3>Potions</h3>
<div class="items">
<div class="item" id="item1">
<img src="../img/aging-potion.png" alt="Potion">
<h6>Aging Potion -</h6> <span>$29.99</span>
</div>
<div class="item" id="item2">
<img src="../img/bulgeye-potion.png" alt="Potion">
  <h6>Bulgeye Potion -</h6> <span>$19.99</span>
</div>
<div class="item" id="item3">
<img src="../img/dragon-tonic.png" alt="Potion">
  <h6>Dragon Tonic -</h6> <span>$64.99</span>
</div>
<div class="item" id="item4">
<img src="../img/love-potion.png" alt="Potion">
  <h6>Love Potion -</h6> <span>$29.99</span>
</div>
<div class="item" id="item5">
<img src="../img/polyjuice-potion.png" alt="Potion">
  <h6>Polyjuice Potion -</h6> <span>$99.99</span>
</div>
<div class="item" id="item6">
<img src="../img/sleeping-draught.png" alt="Potion">
    <h6>Sleeping Draught -</h6> <span>$14.99</span>
</div>

<div class="bg-modal">
<div class="modal-content">
<div class="close">+</div>
<img src="../img/bulgeye-potion.png" alt="" id="modalImg1">

<div class="modalTxt" id="modalTxt1">
  <h4>Aging Potion</h4>
  <h5>Use/Effect:</h5>
  <p>It affects one's eyes, causing them to swell</p>
  <h5>Ingredients:</h5>
  <ul>
  <li>Beetle eyes</li>
  <li>Eel eyes</li>
  </ul>
  <h5>Price:</h5>
  <span class="modalPrice">$29.99</span>
<br>
  <span class="buyBtn">ADD TO CART</span>
</div>
</div>
</div>

</html>


<script type="text/javascript">

document.querySelector('.item').addEventListener('click',
function(){
  document.querySelector('.bg-modal').style.display = 'flex';
  document.querySelector('#modalTxt1').style.display = 'inline-block';
  document.querySelector('#modalImg1').style.display = 'inline-block';
});


document.querySelector('.close').addEventListener('click',
function(){
  document.querySelector('.bg-modal').style.display = "none";
modaltext.style.display = "none";
});
</script>



    .item img{
  width:50px;
}
.bg-modal{
  display: none;
  -ms-align-items: center;
  align-items: center;
  justify-content: center;
  width:100%;
  height:100%;
  background-color:rgba(0, 0, 0, 0.7);
  position: fixed;
  top:0;
}
.modal-content {
  text-align:left;
  position: relative;
border-radius:4px;
  width:80%;
  height:80%;
  background-color:white;
}
.close {
  position: absolute;
  right:14px;
  top:0px;
  font-size:42px;
  transform:rotate(45deg);
  cursor: pointer;
}
.modal-content img {
  width:200px;
  display:none;
}
.buyBtn {
  background-color:red;
  padding: 9px 18px;
  border-radius:4px;
  color:white;
}
.modalPrice{
  color:red;
}
.modalTxt{
  position: absolute;
}

.modal-content{
  width:60%;
}
  .modal-content img{
    width:56%;
    position:absolute;
left:0;
top:0;
bottom:0;
margin:auto;
  }
  .modalTxt{
    right:10%;
    top:50px;
  }
  .modalTxt h4{
    font-size:18pt;
  }
  .modalTxt h5{
    font-size:18pt;
    margin-top:24px;
  }
  .modalTxt p {
    margin-top:24px;
  }
  .modalTxt ul {
    margin-top:24px;
  }
  .modalPrice{
    font-size:18pt;
    line-height:1.5em;
  }
  .buyBtn{
    position: absolute;
    bottom:-50px;
  }

例如,这是您的产品

<div onCLick="showModal(this)">
    <h3>Product Name</h3>
    <p>product description</p>
</div>

现在你需要javascript函数

function showModal(product) {
    // get the product information
    var productName = $(product).find("h3").text();
    var productDescription = $(product).find("p").text();

    var modal = $("#myModal");

    // fill the modal with the information
    $(modal).find(".modal-title").text(productName);
    $(modal).find(".modal-body").text(productDescription);

    // show the modal
    $("#myModal").modal("show");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM