簡體   English   中英

HTML、JavaScript。 如何在同一個彈窗按鈕上顯示不同的信息

[英]HTML, JavaScript. How to display different information on the same popup window button

我想知道如何在同一個彈出窗口上顯示不同的信息?

我正在使用這個例子: https : //www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal

我發現要激活幾個相同的按鈕,我必須編輯 JavaScript 代碼,將按鈕轉換為類。

 <button class="myBtn">butoon 1</button> <button class="myBtn">button 2</button> <button class="myBtn">button 3</button>

JavaScript:

 // Get the modal var modal = document.getElementById('myModal'); // Get the button that opens the modal var btns = document.getElementsByClassName("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks the button, open the modal for (var i = 0; i < btns.length; i++) { btns[i].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"; } }

但是如果我創建幾個相同的按鈕並更改其中的信息,我會得到與第一個彈出窗口中相同的信息。 我該如何解決這個問題?

使用此代碼。
這對我來說很有用。

 var modal = document.getElementById("myModal"); // Get the button that opens the modal var btns = document.getElementsByClassName("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; var modalText = document.getElementById('modal-text'); // When the user clicks the button, open the modal for (var i = 0; i < btns.length; i++) { btns[i].onclick = function(event) { switch(event.target.innerHTML) { case "button 1": modalText.innerHTML = "First button Clicked"; break; case "button 2": modalText.innerHTML = "Second button Clicked"; break; default: modalText.innerHTML = "Third button Clicked"; } 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"; } }
 body {font-family: Arial, Helvetica, sans-serif;} /* The Modal (background) */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 100px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content */ .modal-content { background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; width: 80%; } /* The Close Button */ .close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; }
 <button class="myBtn">button 1</button> <button class="myBtn">button 2</button> <button class="myBtn">button 3</button> <!-- The Modal --> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">&times;</span> <p id="modal-text">Some text in the Modal..</p> </div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM