2016-06-29 5 views
0

여기 두 버튼을 클릭하면 모달 박스가 팝업됩니다.두 개 이상의 모달 박스 한 페이지에

<div id="myModal2" class="modal"> 
    <div class="modal-content"> 
     <span class="close">×</span> 
    <p><h2><font color="black">Week 2</font></h2></p> 
     <p><font color="black"> Example 1 </p> 
    </div> 
</div> 

스크립트 :

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

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

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

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

btn2.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> 
<!-- MODAL INFORMATION FOR WEEK 1 --> 
<div id="myModal" class="modal"> 
    <div class="modal-content"> 
     <span class="close">×</span> 
    <p><h2><font color="black">Week 1</font></h2></p> 
    <div class="modal-body"> 
     <p><font color="black"> ex1</p> 
    </div> 
    </div> 
</div> 

다음 주에 두 모달 정보입니다 :

<li type="button" id="myBtn" class="w3-hide-small w3-center"><a href="#" class="w3-hover-white">1</a></li> 
<li type="button" id="myBtn2" class="w3-hide-small w3-center"><a href="#" class="w3-hover-white">2</a></li> 

다음 주 하나에 대한 모달 정보입니다ID를 변경하면 모달 상자를 다르게 처리 할 수 ​​있으며 "Btn"과 "Btn2"를 다르게 호출 할 수 있다고 생각했습니다. 나는 1과 2를 클릭하면

, 그들은 모두 같은 주 하나에서 텍스트를 열 2. 같은 모달 대상으로하기 때문에 동일한 내용을보고있는 이유는

+0

('display.block' 모달 열리지 않습니다) 버튼에 동일한 data-target으로 모달에 같은 id 필요 . 그리고 HTML은 부트 스트랩 모달과 비슷하지 않습니다. – makshh

+0

대화 상자의 변경 사항이 더 우수합니다. https://jqueryui.com/dialog/#default – HudsonPH

+0

"부트 스트랩을 사용해야합니까?" 부트 스트랩이 부 풀리지 않고 모달을 쉽게 만들 수 있습니다! – BillyNate

답변

1

:

btn.onclick = function() { 
    modal.style.display = "block"; 
} 

btn2.onclick = function() { 
    modal.style.display = "block"; <--- should be modal2 
} 
+0

model2로 변경하면 완전히 작동하지 않게됩니다. 나는 여전히 1을 의미하지만, 2를 클릭해도 아무 일도 일어나지 않습니다. modal2 대신 modal2로 클래스를 변경해 보았습니다. – kgt

+0

나는 modal2에 무슨 일이 일어나고 있는지보기 위해 페이지를 조사 할 것이고, 그것이 작동해야한다고 볼 수있다. –

0

모달에 대한 데이터 속성을 추가하십시오. 당신은

<!-- MODAL INFORMATION FOR WEEK 1 --> 
<div id="myModalA" class="modal"> 
    <div class="modal-content"> 
     <span class="close">×</span> 
    <p><h2><font color="black">Week 1</font></h2></p> 
    <div class="modal-body"> 
     <p><font color="black"> ex1</p> 
    </div> 
    </div> 
</div> 


<div id="myModal2" class="modal"> 
    <div class="modal-content"> 
     <span class="close">×</span> 
    <p><h2><font color="black">Week 2</font></h2></p> 
     <p><font color="black"> Example 1 </p> 
    </div> 
</div> 


<li type="button" id="myBtn" class="w3-hide-small w3-center" data-target="myModalA"><a href="#" class="w3-hover-white">1</a></li> 


<li type="button" id="myBtn2" class="w3-hide-small w3-center" data-target="myModal2"><a href="#" class="w3-hover-white">2</a></li> 
당신은 자바 스크립트에서 조동사를 엽니 다 부트 스트랩의 jQuery 방법을 사용한다
+0

나는 이것이 작동 할 수 있다고 믿지만, 나의 데이터 - 타겟이 등록되지 않은 이상한 문제가있다. 빨간색으로 바뀌어야 할 때 여전히 검은 색 텍스트로 표시됩니다. 메모장 ++ 사용. – kgt

관련 문제