Opening multiple modal boxes on one page

前端 未结 3 1557
萌比男神i
萌比男神i 2021-01-06 03:16

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 wind

3条回答
  •  一生所求
    2021-01-06 04:07

    You must make modals unique to be able to select them later. One way to do this is through id.

    
    
    

    You need to define which button which window should open. One possible solution for this is through data-attributes

    //will open modal one
    //will open modal two

    And then - event:

    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);
    

    Again - this is one possible solution.

提交回复
热议问题