javascript - 模态框不适用于所有引用

标签 javascript html css popup bootstrap-modal

所以我有一个“我们的团队”页面,我想在点击每个成员的名字时显示更多信息。

我有一个模态框代码,但是它只对第一个成员有效。当我点击其他成员的名字时,没有弹出任何内容。

模态框html代码如下:

                <div id="myModal" class="modal">
                    <div class="modal-content">
                        <span class="close">&times;</span>
                        <object width="100%" height="100%" data="bio/*insert member name*.pdf"></object>
                    </div>
                </div>  

现在为每个成员重复此代码,插入成员名称部分更改为显示正确的 pdf。

这是引用模态框的html代码: 成员(member)姓名

这是CSS代码:

.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* 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(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
height: 100%
}

/* The Close Button */
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;

这是javascript代码:

// 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";
    }
}

请帮忙,因为我不知道我做错了什么。

提前谢谢你。

最佳答案

您只共享了一部分代码,但我假设您的循环正在创建模态的 divs

您可以创建一个接收 modalId 数组的函数,并通过 id 应用监听器。

var setupModals = function(modalIds) {
  for (var i = 0; i < modalIds.length; i++) {
    setupModalListener(modalIds[i]);
  }
};

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

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

  // 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";
    }
  };
};

setupModals(['myModal', 'myModal2']);
  .modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* 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(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content/Box */

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  /* 15% from the top and centered */
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
  /* Could be more or less, depending on screen size */
  height: 100%
}


/* The Close Button */

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

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <object width="100%" height="100%" data="bio/*insert member name*.pdf">
    Hello world # 1
    </object>
   
  </div>
</div>

<div id="myModal2" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <object width="100%" height="100%" data="bio/*insert member name*.pdf">
    Hello world # 2
    </object>
    
  </div>
</div>

<button id="myModal_myBtn">Click#1</button>
<button id="myModal2_myBtn">Click#2</button>

希望对您有所帮助!

关于javascript - 模态框不适用于所有引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383927/

相关文章:

css - 使用动画根据内容调整元素大小

javascript - jQuery Mega Menu 全宽菜单项

javascript - 将事件处理程序添加到 Bootstrap 表内的输入字段

javascript - 实例化子对象(对象内的对象)Angular 2

html - 行内元素方向

javascript - 在 Svelte 组件中有没有一种方便的方法来引用 DOM 元素?

css - 如何修复使用 CSS3 媒体查询时不正常的菜单?

javascript - Reactjs:将 React 代码粘合到旧的 javascript 代码中

click - javascript禁用边框半径点击事件

jquery - 子菜单超出页面边距