我想做个html前端代码,点击按钮出现模态弹窗
时间: 2023-05-12 08:05:16 浏览: 85
好的,您可以使用以下代码实现您的需求:
HTML代码:
```
<button onclick="openModal()">点击打开模态框</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeModal()">×</span>
<p>这是一个模态框。</p>
</div>
</div>
```
CSS代码:
```
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
```
JavaScript代码:
```
function openModal() {
document.getElementById("myModal").style.display = "block";
}
function closeModal() {
document.getElementById("myModal").style.display = "none";
}
```
这样,您就可以在点击按钮时打开一个模态框了。