2016-10-31 3 views
0

私はボタンを押して、私が望む内容のポップアップを表示する機能を持っています。あなたはこれを以下で見ることができます。私はこれらの複数を持っているが、コードを変更する方法がわからない。別のポップアップの内容を開くボタンを用意したいと思います。他のボタンにポップアップを適用します。 1つのボタンに対応するコードを持っています

JSコードをコピーして貼り付けて関連IDを変更しましたが、動作しません。誰にでもアイデアはありますか?

ありがとうございます! osyan同様

// 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>

+0

複数のPOP-UPSの複数のIDを使用して、複数の時間それらを呼び出します! – osyan

+0

JSコードでありがとうございます。それはこのようなものでしょうか? var modal = document.getElementById( 'myModal'。 'myModal1'); とし、HTMLでIDを変更しましたか? – Phil

+0

'var modal = document.getElementById( 'myModal1');' 'id =" myModal1 "'を使用してください – osyan

答えて

0

commenに言ったが、あなたがvill変数PRの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>

関連する問題