html实现弹窗编辑资料,《小白HTML5成长之路33》自定义弹窗修改弹窗内容实现方法...
时间: 2023-07-15 14:12:53 浏览: 52
要实现弹窗编辑资料,可以使用HTML、CSS和JavaScript结合的方式来创建自定义弹窗。以下是一个简单的示例:
HTML代码:
```
<div class="popup">
<div class="popup-content">
<div class="popup-header">
<h2>Edit Profile</h2>
<span class="close">×</span>
</div>
<div class="popup-body">
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<button type="submit">Save</button>
</form>
</div>
</div>
</div>
```
CSS代码:
```
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none;
justify-content: center;
align-items: center;
}
.popup-content {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
.popup-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.popup-header h2 {
margin: 0;
}
.close {
font-size: 30px;
cursor: pointer;
}
.popup-body {
margin-top: 20px;
}
label {
display: block;
font-weight: bold;
}
input[type="text"],
input[type="email"],
input[type="password"] {
padding: 5px;
border-radius: 5px;
border: 1px solid #ccc;
width: 100%;
margin-bottom: 10px;
}
button[type="submit"] {
background-color: #4CAF50;
color: #fff;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
```
JavaScript代码:
```
// Get the popup element
var popup = document.querySelector('.popup');
// Get the button that opens the popup
var btn = document.querySelector('.open-popup');
// Get the <span> element that closes the popup
var closeBtn = popup.querySelector('.close');
// When the user clicks the button, open the popup
btn.onclick = function() {
popup.style.display = "flex";
}
// When the user clicks on <span> (x), close the popup
closeBtn.onclick = function() {
popup.style.display = "none";
}
// When the user clicks anywhere outside of the popup, close it
window.onclick = function(event) {
if (event.target == popup) {
popup.style.display = "none";
}
}
```
说明:
1. HTML代码中包含一个`div`元素,它是弹窗的容器,其中包含一个`form`表单,用于输入和提交用户资料。
2. CSS代码中定义了弹窗的样式,包括背景色、边框、圆角、阴影等。
3. JavaScript代码中包含了打开弹窗、关闭弹窗的事件处理程序。
4. 打开弹窗的事件处理程序将`popup`元素的`display`属性设置为`flex`以显示弹窗。
5. 关闭弹窗的事件处理程序将`popup`元素的`display`属性设置为`none`以隐藏弹窗。
6. 当用户点击弹窗以外的区域时,关闭弹窗。
这样,你就可以使用这个示例来实现弹窗编辑资料的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)