如何从不同的按钮触发相同的弹出窗口?
How do I trigger same popup from different buttons?
我试图通过菜单中的不同按钮打开此弹出窗口,但是,弹出窗口内容将保持不变。
谢谢,Banick
function OpenModalT() {
var modal = document.getElementById('myModalT');
modal.style.display = "block";
}
function CloseModalT() {
var modal = document.getElementById('myModalT');
modal.style.display = "none";
}
<li>
<a href="#" onclick="OpenModalT();">Button 1</a>
<!-- The Modal -->
<div id="myModalT" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close" onclick="CloseModalT();">×</span>
<p>
<ppopup>
<br>
<br>
<strong>Dummy text Dummy text Dummy Text <br><br> Kind Regards
</strong>
</ppopup>
</p>
</div>
</div>
</li>
<li>
<a href="#" onclick="OpenModalT();">Button 2</a>
</li>
这是一个示例,您可以使用它来拥有任意数量的模态框和按钮:
- 使用具有唯一 ID 的模式
- 使用
<button type="button" data-modal="#some-modal-id">
切换模态
const EL_modals = document.querySelectorAll(".modal");
const toggleModal = (ev) => {
const EL_btn = ev.currentTarget;
const EL_modal = document.querySelector(EL_btn.dataset.modal);
// Close all currently open modals:
EL_modals.forEach(EL => {
if (EL !== EL_modal) EL.classList.remove("is-active");
});
// Toggle open/close targeted one:
EL_modal.classList.toggle("is-active");
};
const EL_modalBtns = document.querySelectorAll("[data-modal]");
EL_modalBtns.forEach(EL => EL.addEventListener("click", toggleModal));
/*QuickReset*/ * {margin:0; box-sizing: border-box;}
.modal {
position: fixed;
z-index: 99999;
display: flex;
justify-content: center;
align-items: center;
left: 0; top: 0;
width: 100vw; height: 100vh;
transition: 0.4s;
backdrop-filter: blur(3px);
background: rgba(0,0,0,0.1);
pointer-events: none;
visibility: hidden;
opacity: 0;
}
.modal.is-active {
pointer-events: auto;
visibility: visible;
opacity: 1;
}
.modal-content {
position: relative;
background: #fff;
padding: 30px;
box-shadow: 0 5px 20px -5px rgba(0,0,0,0.3);
}
.modal-close {
position: absolute;
top: 10px;
right: 10px;
}
<div id="modal-regards" class="modal">
<div class="modal-content">
<button type="button" data-modal="#modal-regards" class="modal-close">×</button>
<p>Dummy text Dummy text Dummy Text<br>Kind Regards</p>
</div>
</div>
<div id="modal-help" class="modal">
<div class="modal-content">
<button type="button" data-modal="#modal-help" class="modal-close">×</button>
<p>If something is not clear,<br>feel free to ask.</p>
</div>
</div>
<button type="button" data-modal="#modal-regards">Kind regards</button>
<button type="button" data-modal="#modal-regards">Kind regards</button>
<button type="button" data-modal="#modal-help">Ask for help!</button>
此外:
- 避免使用内联 on* 处理程序,因为您希望不要使用内联样式属性 - JS 和 CSS 应该只在一个地方 - 那就是你的文件或标签。
- 使用更好的代码编辑器。
”
与 "
不太一样 - 这样的编辑器会标记并突出显示那些小错别字。
<ppopup>
是无效的 HTML5 标签。 – Roko C. Buljan 16 分钟前
- 如果需要按钮,请不要使用
<a href="#">
。请改用 <button type="button">
。
您需要从 display: none
开始。
var modal = document.getElementById('myModalT');
function OpenModalT() {
modal.style.display = "block";
}
function CloseModalT() {
modal.style.display = "none";
}
function ModalFunction() {
if (modal.style.display === "none") {
modal.style.display = "block";
} else {
modal.style.display = "none";
}
}
/*Add this:*/
#myModalT {
display: none;
}
<li>
<a href="#" onclick="ModalFunction();">Button 1</a>
<!-- The Modal -->
<div id="myModalT" class="modal">
<!-- Modal content -->
<div class="modal-content">
<p>
<ppopup>
<br>
<br>
<strong>Dummy text Dummy text Dummy Text <br><br> Kind Regards
</strong>
<span class="close" onclick="ModalFunction();">×</span>
</ppopup>
</p>
</div>
</div>
</li>
<li>
<a href="#" onclick="ModalFunction();">Button 2</a>
</li>
添加了一些功能:
- 您可以再次点击关闭
- 已保存 Space 于 javascript
我试图通过菜单中的不同按钮打开此弹出窗口,但是,弹出窗口内容将保持不变。
谢谢,Banick
function OpenModalT() {
var modal = document.getElementById('myModalT');
modal.style.display = "block";
}
function CloseModalT() {
var modal = document.getElementById('myModalT');
modal.style.display = "none";
}
<li>
<a href="#" onclick="OpenModalT();">Button 1</a>
<!-- The Modal -->
<div id="myModalT" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close" onclick="CloseModalT();">×</span>
<p>
<ppopup>
<br>
<br>
<strong>Dummy text Dummy text Dummy Text <br><br> Kind Regards
</strong>
</ppopup>
</p>
</div>
</div>
</li>
<li>
<a href="#" onclick="OpenModalT();">Button 2</a>
</li>
这是一个示例,您可以使用它来拥有任意数量的模态框和按钮:
- 使用具有唯一 ID 的模式
- 使用
<button type="button" data-modal="#some-modal-id">
切换模态
const EL_modals = document.querySelectorAll(".modal");
const toggleModal = (ev) => {
const EL_btn = ev.currentTarget;
const EL_modal = document.querySelector(EL_btn.dataset.modal);
// Close all currently open modals:
EL_modals.forEach(EL => {
if (EL !== EL_modal) EL.classList.remove("is-active");
});
// Toggle open/close targeted one:
EL_modal.classList.toggle("is-active");
};
const EL_modalBtns = document.querySelectorAll("[data-modal]");
EL_modalBtns.forEach(EL => EL.addEventListener("click", toggleModal));
/*QuickReset*/ * {margin:0; box-sizing: border-box;}
.modal {
position: fixed;
z-index: 99999;
display: flex;
justify-content: center;
align-items: center;
left: 0; top: 0;
width: 100vw; height: 100vh;
transition: 0.4s;
backdrop-filter: blur(3px);
background: rgba(0,0,0,0.1);
pointer-events: none;
visibility: hidden;
opacity: 0;
}
.modal.is-active {
pointer-events: auto;
visibility: visible;
opacity: 1;
}
.modal-content {
position: relative;
background: #fff;
padding: 30px;
box-shadow: 0 5px 20px -5px rgba(0,0,0,0.3);
}
.modal-close {
position: absolute;
top: 10px;
right: 10px;
}
<div id="modal-regards" class="modal">
<div class="modal-content">
<button type="button" data-modal="#modal-regards" class="modal-close">×</button>
<p>Dummy text Dummy text Dummy Text<br>Kind Regards</p>
</div>
</div>
<div id="modal-help" class="modal">
<div class="modal-content">
<button type="button" data-modal="#modal-help" class="modal-close">×</button>
<p>If something is not clear,<br>feel free to ask.</p>
</div>
</div>
<button type="button" data-modal="#modal-regards">Kind regards</button>
<button type="button" data-modal="#modal-regards">Kind regards</button>
<button type="button" data-modal="#modal-help">Ask for help!</button>
此外:
- 避免使用内联 on* 处理程序,因为您希望不要使用内联样式属性 - JS 和 CSS 应该只在一个地方 - 那就是你的文件或标签。
- 使用更好的代码编辑器。
”
与"
不太一样 - 这样的编辑器会标记并突出显示那些小错别字。 <ppopup>
是无效的 HTML5 标签。 – Roko C. Buljan 16 分钟前- 如果需要按钮,请不要使用
<a href="#">
。请改用<button type="button">
。
您需要从 display: none
开始。
var modal = document.getElementById('myModalT');
function OpenModalT() {
modal.style.display = "block";
}
function CloseModalT() {
modal.style.display = "none";
}
function ModalFunction() {
if (modal.style.display === "none") {
modal.style.display = "block";
} else {
modal.style.display = "none";
}
}
/*Add this:*/
#myModalT {
display: none;
}
<li>
<a href="#" onclick="ModalFunction();">Button 1</a>
<!-- The Modal -->
<div id="myModalT" class="modal">
<!-- Modal content -->
<div class="modal-content">
<p>
<ppopup>
<br>
<br>
<strong>Dummy text Dummy text Dummy Text <br><br> Kind Regards
</strong>
<span class="close" onclick="ModalFunction();">×</span>
</ppopup>
</p>
</div>
</div>
</li>
<li>
<a href="#" onclick="ModalFunction();">Button 2</a>
</li>
添加了一些功能:
- 您可以再次点击关闭
- 已保存 Space 于 javascript