2016-10-31 4 views
0

저는 버튼을 누르면 내가 원하는 컨텐트와 함께 팝업이 표시되는 기능이 있습니다. 아래에서 확인할 수 있습니다. 나는 이것들을 여러 개 갖고 싶지만 코드를 어떻게 바꿀 지 모르겠습니다. 다른 팝업 창을 여는 다른 버튼을 갖고 싶습니다.다른 버튼에 팝업을 적용하십시오. 하나의 버튼에 대해 작동하는 코드가 있습니다.

JS 코드를 복사하여 붙여 넣었으며 관련 ID를 변경했지만 작동하지 않습니다. 누구든지 아이디어가 있습니까?

감사합니다. osyan처럼

+0

사용 여러 여러 팝업을 위해 ID와 여러 시간을 전화! – osyan

+0

JS 코드에서 감사합니다. 그것은 이렇게 될 것입니다. var modal = document.getElementById ('myModal'. 'myModal1'); 그런 다음 ID가 HTML로 변경 되었습니까? – Phil

+0

'var modal = document.getElementById ('myModal1');''id = "myModal1" '을 사용합니다. – osyan

답변

0

// 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 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"; 
 
    } 
 
    }
HTML (Button): 
 

 
    <button class="buttin" id="myBtn">Terms & Conditions</button> 
 

 
HTML (Pop Up Content): 
 

 
    <div id="myModal" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close">×</span> 
 
     <h3 style="margin-top: 0px;"><strong>Terms and Conditions</strong></h3> 
 
    Please read the following terms and conditions before using our website. 
 
    </div> 
 
    </div>

은 commen에서 말했다,하지만 당신은 VILL 변수 홍보 ID가 필요합니다. 같은 :

var modal = document.getElementById('myModal1'); 
var modal2 = document.getElementById('myModal2'); 

전체 예를 들어 https://jsfiddle.net/3u8ummwn/2/를 참조

0

당신은 그렇게 할 수 있습니다 : 버튼에 데이터 속성 = "modalId"를 추가하고 클릭에 해당 ID를 얻을, 그 ID로 모달의 DOM을 얻을

// Get the modal 
 
    
 

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

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

 
    // When the user clicks anywhere outside of the modal, close it 
 
    window.onclick = function(event) { 
 
     
 
     if(event.target.className=="close") 
 
     return; 
 
     
 
     var modal = document.getElementById(event.target.getAttribute("data-modal")); 
 
     
 
     if (event.target != modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
     
 
     if (event.target.hasAttribute("data-modal")) { 
 
     modal.style.display = "block"; 
 
    } 
 
     
 
     
 
    }
HTML (Button): 
 

 
    <button class="buttin" data-modal="myModal">Terms & Conditions</button> 
 

 
HTML (Pop Up Content): 
 

 
    <div id="myModal" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close">×</span> 
 
     <h3 style="margin-top: 0px;"><strong>Terms and Conditions</strong></h3> 
 
    Please read the following terms and conditions before using our website. 
 
    </div> 
 
    </div> 
 

 
<br/> 
 
<button class="buttin" data-modal="myModal1">New Button</button> 
 

 
    <div id="myModal1" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close" >×</span> 
 
     <h3 style="margin-top: 0px;"><strong>Terms and Conditions 11111</strong></h3> 
 
    Please read the following terms and conditions before using our website. 
 
    </div> 
 
    </div>

관련 문제