6

I have a page that needs to have different modal boxes open when their corresponding link on the page is clicked. I've got the structure and javascript to work for ONE window, but I can't get it to work for more than one. I am thinking I need to loop through each modal box...but can't quite figure out the syntax.

This is for a client's WordPress site, and I'm using Advanced Custom Fields to populate the content. The page isn't published yet, so here's a codepen that shows my code: http://codepen.io/FelixB/pen/EPvEVG

The code:

var modal = document.getElementsByClassName('modal-window');

var btn = document.getElementsByClassName("click-to-open");

var span = document.getElementsByClassName("close")[0];

for (var i = 0; i < btn.length; i++) {
  var thisBtn = btn[i]
  thisBtn.onclick = function() {
    alert("hello");

    //modal.style.display = "block";
  }
}

span.onclick = function() { 
  modal.style.display = "none";
}

window.onclick = function(event) {

}
4

3 回答 3

16

您必须使模态框独一无二,以便以后能够选择它们。一种方法是通过id.

<div id="modal-window-one" class="modal-window modal"></div>
<div id="modal-window-two" class="modal-window modal"></div>

您需要定义哪个按钮应该打开哪个窗口。一种可能的解决方案是通过data-attributes

<div class="click-to-open" data-modal="modal-window-one"> //will open modal one
<div class="click-to-open" data-modal="modal-window-two"> //will open modal two

然后 - 事件:

var btn = document.getElementsByClassName("click-to-open");

for (var i = 0; i < btn.length; i++) {
  var thisBtn = btn[i];
  thisBtn.addEventListener("click", function(){
    var modal = document.getElementById(this.dataset.modal);
    modal.style.display = "block";
}, false);

再次 - 这是一种可能的解决方案。

于 2016-01-14T19:38:05.643 回答
1

下面是一个使用引导程序显示多个弹出窗口的示例,还附加了展示示例的 jsfiddle。为模态对话框设置一个类名。

<div class="modal fade myModal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <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>
    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
  </div>
  <div class="modal-body">
    ...
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary">Save changes</button>
  </div>
</div>  </div></div>

实例化模态对话框。

$('.myModal').modal();

下面是一个正在运行的 jsfiddle https://jsfiddle.net/niteshp27/msdg98vn/

于 2017-02-01T11:40:10.243 回答
0

所有模态的类将保持不变,class="modal"但是当您希望在同一屏幕上显示多个模态时,请使用该"id"属性,因为 Bootstrap 模态是用 Javascript 编写的,而 JS 完全基于 id

<div id="modal-one" class="modal fade"></div>
<div id="modal-two" class="modal fade"></div>

data-target您需要使用<button> 标签的属性来定义哪个按钮将针对哪个模式。如果您使用 < a > 标签来触发模态,请使用该href属性。例子 :

<button data-toggle="modal" data-target="#modal-one"> First Modal </button>          
<button data-toggle="modal" data-target="#modal-two"> Second Modal </button>

或者,如果使用 < a > 标签,则;

<a data-toggle="modal" href="#modal-one"> First Modal </a>
<a data-toggle="modal" href="#modal-one"> First Modal </a>

有关更多信息,您可以查看以下页面Bootstrap Modals。

注意:根据 Bootstrap 的官方文档 Bootstrap 一次只支持一个模式窗口。不支持嵌套模式,因为他们认为它们会导致糟糕的用户体验。

于 2018-07-20T11:47:31.967 回答