【问题标题】:Modal box works for one element but not for others模态框适用于一个元素,但不适用于其他元素
【发布时间】:2017-09-07 09:07:00
【问题描述】:

我创建了一个可以正常工作的模态框,但是当我将它应用于其他元素时,它只适用于第一个元素。 这些元素是不同的图像,当您单击打开一个模态框时,每个图像中都有不同的信息

这是url

如果有人能帮我解决这个问题,我将不胜感激。

谢谢,

这是 HTML:

<div id="myBtn">
  <div class="container-team">
    <img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/image-normal.jpg" class="image-person">
    <img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/image-hover.jpg" class="overlay-person">
    <div class="centered">Jack Denton</div>
  </div>
</div>


<!-- The Modal -->
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">×</span>
    <aside>
      <div class="photo-person-modal-box">
        <img src="https://i.stack.imgur.com/7UL6j.jpg">
      </div>

      <div class="connect-modal-box">
        <div class="social-icon-modal-box">
          <div class="container-social-icon">
            <a href="mailto:jack.denton@allaboutgroup.org" ><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/email-white.png" width="40px" height="40px" class="icon-normal"></a>
            <a href="mailto:jack.denton@allaboutgroup.org" ><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/email-white-hover-f.png" width="40px" height="40px" class="icon-hover"></a>
          </div>


          <div class="container-social-icon">
            <a href="tel:0203-651-4919"><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/phone-white.png" width="40px" height="40px" class="icon-normal"></a>
            <a href="tel:0203-651-4919"><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/phone-white-hover-f.png" width="40px" height="40px" class="icon-hover"></a>
          </div>


          <div class="container-social-icon">
            <a href="https://www.linkedin.com/in/jackwilliamdenton/" target="blank"><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/linkedin-white.png" width="40px" height="40px" class="icon-normal"></a>
            <a href="https://www.linkedin.com/in/jackwilliamdenton/" target="blank"><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/linkedin-white-hover-f.png" width="40px" height="40px"  class="icon-hover"></a>
          </div>


          <div class="container-social-icon">
            <a href="https://twitter.com/Jack_AAG" target="blank"><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/twitter-white.png" width="40px" height="40px" class="icon-normal"></a>
            <a href="https://twitter.com/Jack_AAG" target="blank"><img src="http://allaboutgroup.isabelsaez.co.uk/wp-content/uploads/2017/09/twitter-white-hover-f.png" width="40px" height="40px"  class="icon-hover"></a>
          </div>

        </div>
      </div>
    </aside>

    <div class="description-modal-box">
      <header class="header-modal-box">
        <div class="name-modal-box">
          <strong>JACK DENTON</strong>
        </div>
        <div class="role-modal-box"> CO-FOUNDER</div>
      </header>

      <div class="content-modal-box">
        <div class="quote-modal-box">The big dog, the raconteur, the linguaphile.
        </div>
        <div class="question-modal-box">Worst job after graduating?</div>
        <div class="text-modal-box">Working on a chicken farm. Have you seen Napoleon Dynamite? It’s like that but worse. Dead chickens in bins a metre from where I was working, stifling heat and constant attacks from angry cockerels. It was alright, though, because they were paying me £3 an hour.
        </div>
      </div>
    </div>
  </div>
</div>

这是 JavaScript:

<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

</script>

这是 CSS:

/*__________________MODALBOX__________*/



/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 4; /* Sit on top */
    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(3,134,221,0.30); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #ffffff;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 0px;
    width: 70%; /* Could be more or less, depending on screen size */
height: 28.75rem;
}

/* The Close Button */
.close {
color: #ffffff;
    float: right;
    font-size: 50px;
    font-weight: bold;
    height: 60px;
    width: 60px;
    position: relative;
    background-color: #019ee3;
    border-radius: 50%;
    box-shadow: rgba(35,35,35,0.2) -1px 1px 8px ;
    padding: 15px 15px;
    text-decoration: none;
    margin: -20px -20px 20px;
transition: all 1s ease-in-out 0s;

}

.close:hover, .close:focus {
    text-decoration: none;
    cursor: pointer;
transform: rotate(360deg);
    transition: all 1s ease-in-out 0s;

}

【问题讨论】:

  • 您能描述一下您尝试解决遇到的问题的方法吗?
  • 您将按钮 ID 分配给所有 myBtn。 id 应该是唯一的不同的
  • 检查了网站。只有最后一个 Div 具有附加事件的 id。我为所有其他 div 提供了相同的 ID,它起作用了。顺便说一句,如果你想要一个通用标识符,你应该使用类而不是 ID。 class="myBtn" 在所有 div 上
  • 感谢它现在有效。我为每个 ID 命名不同。首先,我更改了一个类的 ID,但由于某种原因,我不知道模态框不起作用。但是命名每个元素 ID 会有所帮助

标签: javascript jquery modal-window


【解决方案1】:

我认为你需要像使用 Class 一样使用 myBtn

<div class="myBtn"> 

...

【讨论】:

    猜你喜欢
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多