我正在創建兩個模態。一個是買的,一個是賣的。但是,當我單擊應該關閉它的模態中的跨度時,模態并未關閉。這是代碼:
var buyModal = document.getElementById("buyModal");
var sellModal = document.getElementById("sellModal");
function buy() {
buyModal.style.display = "block";
}
function sell() {
sellModal.style.display = "block";
}
function close() {
buyModal.style.display = "none";
sellModal.style.display = "none";
}
<button id="buyButton" onclick="buy()">Buy a Currency Pair</button>
<button id="sellButton" onclick="sell()">Sell a Currency Pair</button>
<div id="buyModal" class="modal">
<div class="modal-content">
<button class="close" onclick="close()">×</button>
<p>Buy</p>
</div>
</div>
<div id="sellModal" class="modal">
<div class="modal-content">
<button class="close" onclick="close()">×</button>
<p>Sell</p>
</div>
</div>
</div>
有誰知道為什么它不起作用?
uj5u.com熱心網友回復:
Close 是一個保留的 JavaScript 詞,瀏覽器拒絕在事件中呼叫它。
您必須選擇一個新名稱(例如closeDiv):
var buyModal = document.getElementById("buyModal");
var sellModal = document.getElementById("sellModal");
function buy() {
buyModal.style.display = "block";
}
function sell() {
sellModal.style.display = "block";
}
function closeDiv() {
buyModal.style.display = "none";
sellModal.style.display = "none";
}
<button id="buyButton" onclick="buy()">Buy a Currency Pair</button>
<button id="sellButton" onclick="sell()">Sell a Currency Pair</button>
<div id="buyModal" class="modal">
<div class="modal-content">
<button class="close" onclick="closeDiv()">×</button>
<p>Buy</p>
</div>
</div>
<div id="sellModal" class="modal">
<div class="modal-content">
<button class="close" onclick="closeDiv()">×</button>
<p>Sell</p>
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324890.html
標籤:javascript html
