【问题标题】:Make different links with same modal displays different carousels each one使用相同的模态制作不同的链接,每个链接显示不同的轮播
【发布时间】:2021-01-25 08:27:22
【问题描述】:

我正在开发一个有多个链接的网站。单击时的每个链接都显示具有不同轮播的相同模式。我试图将 id 传递给链接的数据目标,但是当打开模式时,它会播放两个轮播而不是仅选定的轮播。我正在尝试找出问题所在,但我不知道,您有什么建议吗?谢谢。

<!doctype html>

<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>
<div class="row" id="gallery">
  <div data-toggle="modal" data-target="#exampleModal" class="col-12 col-sm-6 col-lg-3 d-flex align-self-center bg-info justify-content-center"> <a href="" data-target="#carouselExample1" data-slide-to="0">
    <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-asterisk" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path fill-rule="evenodd" d="M8 0a1 1 0 0 1 1 1v5.268l4.562-2.634a1 1 0 1 1 1 1.732L10 8l4.562 2.634a1 1 0 1 1-1 1.732L9 9.732V15a1 1 0 1 1-2 0V9.732l-4.562 2.634a1 1 0 1 1-1-1.732L6 8 1.438 5.366a1 1 0 0 1 1-1.732L7 6.268V1a1 1 0 0 1 1-1z" />
    </svg>
    </a> </div>
  <div data-toggle="modal" data-target="#exampleModal" class="col-12 col-sm-6 col-lg-3 d-flex align-self-center bg-info justify-content-center"> <a href="" data-target="#carouselExample1" data-slide-to="1">
    <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-asterisk" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path fill-rule="evenodd" d="M8 0a1 1 0 0 1 1 1v5.268l4.562-2.634a1 1 0 1 1 1 1.732L10 8l4.562 2.634a1 1 0 1 1-1 1.732L9 9.732V15a1 1 0 1 1-2 0V9.732l-4.562 2.634a1 1 0 1 1-1-1.732L6 8 1.438 5.366a1 1 0 0 1 1-1.732L7 6.268V1a1 1 0 0 1 1-1z" />
    </svg>
    </a> </div>
  <div data-toggle="modal" data-target="#exampleModal" class="col-12 col-sm-6 col-lg-3 d-flex align-self-center bg-info justify-content-center"> <a href="" data-target="#carouselExample2" data-slide-to="5">
    <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-asterisk" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path fill-rule="evenodd" d="M8 0a1 1 0 0 1 1 1v5.268l4.562-2.634a1 1 0 1 1 1 1.732L10 8l4.562 2.634a1 1 0 1 1-1 1.732L9 9.732V15a1 1 0 1 1-2 0V9.732l-4.562 2.634a1 1 0 1 1-1-1.732L6 8 1.438 5.366a1 1 0 0 1 1-1.732L7 6.268V1a1 1 0 0 1 1-1z" />
    </svg>
    </a> </div>
  <div data-toggle="modal" data-target="#exampleModal" class="col-12 col-sm-6 col-lg-3 d-flex align-self-center bg-info justify-content-center"> <a href="" data-target="#carouselExample2" data-slide-to="6">
    <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-asterisk" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path fill-rule="evenodd" d="M8 0a1 1 0 0 1 1 1v5.268l4.562-2.634a1 1 0 1 1 1 1.732L10 8l4.562 2.634a1 1 0 1 1-1 1.732L9 9.732V15a1 1 0 1 1-2 0V9.732l-4.562 2.634a1 1 0 1 1-1-1.732L6 8 1.438 5.366a1 1 0 0 1 1-1.732L7 6.268V1a1 1 0 0 1 1-1z" />
    </svg>
    </a> </div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button>
      </div>
      <div class="modal-body">
        <div id="carouselExample1" class="carousel slide" data-ride="carousel">
          <ol class="carousel-indicators">
            <li data-target="#carouselExample" data-slide-to="0" class="active"></li>
            <li data-target="#carouselExample" data-slide-to="1"></li>
            <li data-target="#carouselExample" data-slide-to="2"></li>
            <li data-target="#carouselExample" data-slide-to="3"></li>
          </ol>
          <div class="carousel-inner">
            <div class="carousel-item active"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1599472566844-691ad0ac7e41?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="First slide"> </div>
            <div class="carousel-item"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1599442996649-9aaa85137482?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="Second slide"> </div>
            <div class="carousel-item"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1546111380-cfca9a43dd85?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="Third slide"> </div>
            <div class="carousel-item"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1547288242-f3d375fc7b5f?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="Fourth slide"> </div>
          </div>
          <a class="carousel-control-prev" href="#carouselExample" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExample" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div>
        <div id="carouselExample2" class="carousel slide" data-ride="carousel">
          <ol class="carousel-indicators">
            <li data-target="#carouselExample" data-slide-to="4" class="active"></li>
            <li data-target="#carouselExample" data-slide-to="5"></li>
            <li data-target="#carouselExample" data-slide-to="6"></li>
            <li data-target="#carouselExample" data-slide-to="7"></li>
          </ol>
          <div class="carousel-inner">
            <div class="carousel-item active"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1600497299298-9d99c0db6154?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="First slide"> </div>
            <div class="carousel-item"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1601682633973-a76aa17a7844?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="Second slide"> </div>
            <div class="carousel-item"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1546111380-cfca9a43dd85?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="Third slide"> </div>
            <div class="carousel-item"> <img class="d-block w-100" src="https://images.unsplash.com/photo-1547288242-f3d375fc7b5f?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="Fourth slide"> </div>
          </div>
          <a class="carousel-control-prev" href="#carouselExample" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExample" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> 
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 
<script>
        $("#exampleModal").on("show.bs.modal", function (event) {
  var a = $(event.relatedTarget); // Button that triggered the modal
  var recipient = a.data("div"); //  Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this);
  modal.find("#id").attr(".carousel");
  modal.find(".modal-body div");
});

        </script>
</body>
</html>

【问题讨论】:

    标签: twitter-bootstrap bootstrap-4 modal-dialog bootstrap-modal


    【解决方案1】:

    您可以使用 JavaScript 动态更改您的 &lt;div&gt;s 的可见性。

    首先,将 onclick 函数添加到所有四个 &lt;a&gt; 标签:

    <a href="" onclick="showContent(this)" data-target="#carouselExample1" data-slide-to="0">
    

    当你点击它时,这将调用一个函数。

    然后,将以下函数添加到您的 &lt;script&gt;

    function showContent(object){
      if (object.dataset.target == '#carouselExample1' ){
            document.getElementById("carouselExample2").style.display = "none";
            document.getElementById("carouselExample1").style.display = "block";
        }
        else{
            document.getElementById("carouselExample1").style.display = "none";
            document.getElementById("carouselExample2").style.display = "block";
        }
    }
    

    上面的函数会检查你点击的链接的data-target,并根据它显示/隐藏div。

    我建议检查 JavaScript HTML DOM 事件以获取更多信息。

    【讨论】:

    • 我会尽快尝试您的解决方案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多