gpt4 book ai didi

javascript - 使用是/否命令为 Web 创建模式

转载 作者:行者123 更新时间:2023-11-28 19:19:47 24 4
gpt4 key购买 nike

现在,我有一张卡片供人们添加、编辑或删除设备,并带有后续的 FAS 字体超赞图标。基本上,我想知道如何在有人单击其中一个图标时创建弹出命令或选项。例如,如果有人要单击“删除”符号,弹出窗口将包含文本、是和没有按钮。我被卡住的地方是试图用是和否按钮关闭模式,以及如何为是命令提交信息。另外,我正在尝试更改按钮的样式(边框、背景颜色)并卡住。

目前,我的代码示例可以访问 w3schools.com-- https://www.w3schools.com/code/tryit.asp?filename=G77OAKK28TTH

// 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";
}
}
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;
}
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p>Would you like to delete this device?</p>
<button>Yes</button><button>No</button>
</div>
</div>

最佳答案

我将 id 添加到您的按钮,此代码用于在单击 id 为“yes-button”的按钮时关闭模式:

document.getElementById("yes-button").onclick = function() {
modal.style.display = "none";
}

// 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";
}
}

// Close modal when button with id "yes-button" is clicked
document.getElementById("yes-button").onclick = function() {
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;
}
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p>Would you like to delete this device?</p>
<button id="yes-button">Yes</button><button id="no-button">No</button>
</div>
</div>

关于javascript - 使用是/否命令为 Web 创建模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57563526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com