2016-12-31 37 views
1

我沒有在我上一篇文章中解釋我的問題。如何添加第二個模態到我的html頁面

所以我想添加第二個模態到我的html頁面,所以如果你點擊「按鈕1」它會打開「模態1」,如果你點擊「按鈕2」它將打開「模態2」,然而「按鈕3「」按鈕4「」按鈕5「和」按鈕6「打開」模式2「。當我創建第二個模式並設置下面的JavaScript。它將保持鍵上的兩個開口的模態2和按鈕不是模態11.

還要注意,第一按鈕的ID「自由」

還要注意,第二個按鈕「的ID 「當按鈕1被設定爲 」MOD1天「

<script> 
 
// Get the modal1 
 
var modal = document.getElementById('myModal1'); 
 

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

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

 
// When the user clicks on 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"; 
 
    } 
 
} 
 
</script> 
 
<script> 
 
// Get the modal2 
 
var modal = document.getElementById('myModal2'); 
 

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

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

 
// When the user clicks on 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"; 
 
    } 
 
} 
 
</script>

兩個按鈕一直打開」 Modal2 AL1" HTML下面

按鈕1

<!-- service item --> 
 
<div class="col-md-4 wow fadeInLeft" data-wow-duration="500ms"> 
 
    <div class="service-item"> 
 
    <div class="service-icon"> 
 
     <i class="fa fa-snowflake-o fa-2x"></i> 
 
    </div> 
 

 
    <div class="service-desc"> 
 
     <button id="free"> 
 
     <h3>5 Day Free Trial</h3> 
 
     </button> 
 
     <b><p>So you can get a taste of the action for free, we will give new users a 5 day trial to see how they like our bot before they buy!</p> </b> 
 
    </div> 
 
    </div> 
 
</div> 
 
<!-- end service item -->

按鈕2

<!-- service item --> 
<div class="col-md-4 wow fadeInUp" data-wow-duration="500ms" data-wow-delay="500ms"> 
    <div class="service-item"> 
    <div class="service-icon"> 
     <i class="fa fa-clock-o fa-2x"></i> 
    </div> 

    <div class="service-desc"> 
     <button id="oneday"> 
     <h3>1 Day</h3> 
     </button> 
     <b><p>$0.74 &nbsp;&nbsp;-24 Hours </p> </b> 
    </div> 
    </div> 
</div> 
<!-- end service item --> 

而情態動詞在這裏

Modal1

<!-- Modal1 --> 
 
<div id="myModal1" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 

 
    <form name="getinfo" onsubmit="return validateForm()" action="php/gmail.php" method="POST"> 
 
     <div class="form-style-8"> 
 
     <span class="close">&times;</span> 
 
     <label for="msg">E-mail:</label> 
 
     <input type="email" id="mail" name="email" /> 
 
     </div> 
 
     <div> 
 
     <label for="msg">Username:</label> 
 
     <br> 
 
     <input id="user" name="username"> 
 
     </textarea> 
 
     </div> 
 
     <div> 
 
     <label for="msg">Password:</label> 
 
     <br> 
 
     <input type="password" id="pass" name="password"> 
 
     </textarea> 
 
     </div> 
 
     <div> 
 
     <label for="msg">Confirm Password:</label> 
 
     <br> 
 
     <input type="password" id="pass" name="cpassword"> 
 
     </textarea> 
 
     </div> 
 
     <div> 
 
     <label for="msg">3 Hashtags:</label> 
 
     <br> 
 
     <input id="tags" name="hashtags"> 
 
     </textarea> 
 
     </div> 
 
     <div> 
 
     <input id="submit" type="submit" name="submit" value="submit"> 
 
     </div> 
 
    </form> 
 
    </div> 
 
</div>

Modal2

<!-- Modal2 content --> 
 
<div class="modal-content"> 
 

 
    <form name="getinfo2" onsubmit="return validateForm()" action="php/gmail.php" method="POST"> 
 
    <div class="form-style-8"> 
 
     <span class="close">&times;</span> 
 
     <label for="msg">Example:</label> 
 
     <input type="email" id="e1" name="email" /> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example2:</label> 
 
     <br> 
 
     <input id="e2" name="username"> 
 
     </textarea> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example3:</label> 
 
     <br> 
 
     <input type="password" id="e3" name="password"> 
 
     </textarea> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example4:</label> 
 
     <br> 
 
     <input type="password" id="e4" name="cpassword"> 
 
     </textarea> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example5:</label> 
 
     <br> 
 
     <input id="e5" name="hashtags"> 
 
     </textarea> 
 
    </div> 
 
    <div> 
 
     <input id="submit" type="submit" name="submit" value="submit"> 
 
    </div> 
 
    </form> 
 
</div> 
 
</div>

,所以我問我如何才能modal1只與對接工作on1和modal2與我將在稍後創建的按鈕2,3,4,5和6一起工作。

+2

看來你是使用CSS bootsrap,你不使用bootsrap.js?如果你是你不需要手動隱藏/顯示模態,並可以使用他們的API他們 – charlietfl

+0

我使用的是bootsrap.js但是我是新的HTML編碼,所以我不知道很多關於它 –

+1

使用不同的變量來存儲參考模態。 –

回答

0

你只需要一個模式的一個例子 - 它只是一個DIV結構,沒有什麼神奇(唯一magic是使它出現/消失 - 並且bootstrap會這樣做)。需要時,從存儲的地方抓取所需的內容,將其粘貼到模塊的.modal-body div中,然後讓引導程序顯示它。

以下是操作方法。

(1)針對文檔中其他地方的隱藏DIV中的每個模態存儲內容。 (2)給每個按鈕一個帶有唯一編號的ID,並給每個「隱藏模式內容div」一個以相同編號結尾的名字。例如,按鈕「#btn_2」將與隱藏div「#mdl2」匹配,其中包含您想要按鈕#btn_2的內容。 (3)使用javascript(你正在使用Bootstrap,它使用jQuery,所以爲什麼不使用jQuery)交換正確的內容。當您單擊按鈕,隔離數字2(var buttNum = this.id.split('_')[1]; - 嘗試),並用它來抓住從#mdl2 DIV(var content = $('#mdl'+buttNum).html())的HTML

(4)讓引導採取開放的護理/關閉模式。

例子:

$('[id^=btn_]').click(function(){ 
 
    var buttNum = this.id.split('_')[1]; 
 
    //alert(buttNum); 
 
    var content = $('#mdl'+buttNum).html(); 
 
    $('#myModal1 .modal-body').html(content); 
 
});
#mdl1, #mdl2, #mdl3{display:none;} /* Hide the divs containing modal content */
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <title>Bootstrap Example</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 

 
<div class="container"> 
 
    <h3>Re-Using the Same Modal</h3> 
 
    <!-- Trigger the modal with a button --> 
 
    <button id="btn_1" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal One</button> 
 
    <button id="btn_2" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal Two</button> 
 
    <button id="btn_3" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Ogden Nash Poem</button> 
 

 
    <!-- Re-Usable Modal --> 
 
    <div class="modal fade" id="myModal1" role="dialog"> 
 
    <div class="modal-dialog modal-md"> 
 
     <div class="modal-content"> 
 
     <div class="modal-body"> 
 
      
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
    <!-- Stick your modal content in hidden divs at bottom of the page --> 
 
    <div id="mdl1"> 
 
    <div>Click outside modal to close modal</div> 
 
    <form name="getinfo" onsubmit="return validateForm()" action="php/gmail.php" method="POST"> 
 
     <div class="form-style-8"> 
 
     <span class="close">&times;</span> 
 
     <label for="msg">E-mail:</label> 
 
     <input type="email" id="mail" name="email" /> 
 
     </div> 
 
     <div> 
 
     <label for="msg">Username:</label> 
 
     <br> 
 
     <input id="user" name="username"> 
 

 
     </div> 
 
     <div> 
 
     <label for="msg">Password:</label> 
 
     <br> 
 
     <input type="password" id="pass" name="password"> 
 
     </div> 
 
     <div> 
 
     <label for="msg">Confirm Password:</label> 
 
     <br> 
 
     <input type="password" id="pass" name="cpassword"> 
 
     </div> 
 
     <div> 
 
     <label for="msg">3 Hashtags:</label> 
 
     <br> 
 
     <input id="tags" name="hashtags"> 
 
     </div> 
 
     <div> 
 
     <input id="submit" type="submit" name="submit" value="submit"> 
 
     </div> 
 
    </form> 
 
    </div> 
 
    <div id="mdl2"> 
 
     <div>Click outside modal to close modal</div> 
 
     <h1>Image of an Animal</h1> 
 
     <img src="http://placeimg.com/200/200/animals" /> 
 
    </div><!-- #mdl2 --> 
 
    <div id="mdl3"> 
 
     <div>Click outside modal to close modal</div> 
 
     <h1>Poem by Ogden Nash</h1> 
 
     <div style="font-size:1.4rem;"> 
 
\t \t <div>The hands of the clock were reaching high</div> 
 
\t \t <div>In an old midtown hotel;</div> 
 
\t \t <div>I name no name, but its sordid fame</div> 
 
\t \t <div>Is table talk in hell.</div> 
 
\t \t <div>I name no name, but hell's own flame</div> 
 
\t \t <div>Illumes the lobby garish,</div> 
 
\t \t <div>A gilded snare just off Times Square</div> 
 
\t \t <div>For the maidens of the parish.</div> 
 

 
\t \t <div>The revolving door swept the grimy floor</div> 
 
\t \t <div>Like a crinoline grotesque,</div> 
 
\t \t <div>And a lowly bum from an ancient slum</div> 
 
\t \t <div>Crept furtively past the desk.</div> 
 
\t \t <div>His footsteps sift into the lift</div> 
 
\t \t <div>As a knife in the sheath is slipped,</div> 
 
\t \t <div>Stealthy and swift into the lift</div> 
 
\t \t <div>As a vampire into a crypt.</div> 
 
     </div> 
 
    </div><!-- #mdl3 --> 
 
</body> 
 
</html>

1

你不需要寫JavaScript來隱藏或者顯示情態動詞使用正確的數據對象和數據切換的按鈕標籤如下圖所示,自舉會自動做休息。

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Large Modal</button>

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Large Modal</button>

如上所示設置data-toggle="modal"data-target='<your-modal-id>'按鈕標記內。

顯示兩個模式的兩個按鈕,只需添加新模式的ID在data-target='<your-modal-id>'屬性在新的按鈕。

現在創建多個模態,並給他們每個人的不同<your_modal_id>

<div class="modal fade" id="<your_modal_id>" role="dialog"> 
    <div class="modal-dialog modal-lg"> 
     <div class="modal-content"> 
     <div class="modal-body"> 

      YOUR MODAL HTML CODE 

     </div> 
    </div> 
    </div> 

更新:ADDING關閉按鈕關閉模態基於Bootstrap

更換<span class="close">&times;</span>在你的模式與

<button type="button" class="close" data-dismiss="modal">&times;</button>

it將通過點擊模式上的關閉按鈕自動關閉模式。

下面是如何使用兩個模態

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <title>Bootstrap Example</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 

 
<div class="container"> 
 
    <h2>Large Modal</h2> 
 
    <!-- Trigger the modal with a button --> 
 
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Large Modal</button> 
 
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Large Modal</button> 
 

 
    <!-- Modal 1 --> 
 
    <div class="modal fade" id="myModal1" role="dialog"> 
 
    <div class="modal-dialog modal-lg"> 
 
     <div class="modal-content"> 
 
     <div class="modal-body"> 
 
      
 
    \t \t <form name="getinfo" onsubmit="return validateForm()" action="php/gmail.php" method="POST"> 
 
     \t \t <div class="form-style-8"> 
 
     \t \t <button type="button" class="close" data-dismiss="modal">&times;</button> 
 
     \t \t <label for="msg">E-mail:</label> 
 
     \t \t <input type="email" id="mail" name="email" /> 
 
     \t \t </div> 
 
     \t \t <div> 
 
     \t \t <label for="msg">Username:</label> 
 
     \t <br> 
 
     \t <input id="user" name="username"> 
 
     
 
     \t \t </div> 
 
     \t \t <div> 
 
     \t \t <label for="msg">Password:</label> 
 
     \t <br> 
 
     \t \t <input type="password" id="pass" name="password"> 
 
     \t \t </div> 
 
     \t \t <div> 
 
       <label for="msg">Confirm Password:</label> 
 
       <br> 
 
       <input type="password" id="pass" name="cpassword"> 
 
     \t \t </div> 
 
     \t \t <div> 
 
       <label for="msg">3 Hashtags:</label> 
 
       <br> 
 
       <input id="tags" name="hashtags"> 
 
     \t </div> 
 
      <div> 
 
      <input id="submit" type="submit" name="submit" value="submit"> 
 
      </div> 
 
    \t </form> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<!-- Modal 2--> 
 
    <div class="modal fade" id="myModal2" role="dialog"> 
 
    <div class="modal-dialog modal-lg"> 
 
     <div class="modal-content"> 
 
     <div class="modal-body"> 
 
      <form name="getinfo2" onsubmit="return validateForm()" action="php/gmail.php" method="POST"> 
 
    <div class="form-style-8"> 
 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
 
     <label for="msg">Example:</label> 
 
     <input type="email" id="e1" name="email" /> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example2:</label> 
 
     <br> 
 
     <input id="e2" name="username"> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example3:</label> 
 
     <br> 
 
     <input type="password" id="e3" name="password"> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example4:</label> 
 
     <br> 
 
     <input type="password" id="e4" name="cpassword"> 
 
    </div> 
 
    <div> 
 
     <label for="msg">Example5:</label> 
 
     <br> 
 
     <input id="e5" name="hashtags"> 
 
    </div> 
 
    <div> 
 
     <input id="submit" type="submit" name="submit" value="submit"> 
 
    </div> 
 
    </form> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
</body> 
 
</html>

+0

謝謝Nadir!但現在,我實現了我的真正purchase.html。它打開兩個按鈕的模式權限。但是當它關​​閉並重新打開時。它不會顯示第二次 –

+0

'span.onclick = function(){ modal.style.display =「none」; } 你是否刪除了你之前存在的javascript 上面的代碼隱藏了模式,你不需要任何javascript來隱藏或顯示引導程序會爲你做。 –

+0

查看我的更新回答,並用您的模式替換' ×' ''通過點擊模式上的關閉按鈕自動關閉模式 –

相關問題