当然,以下是一个简单的JavaScript点击弹窗的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Click Popup Example</title>
</head>
<body>
<button id="popupButton">点击我</button>
<script src="script.js"></script>
</body>
</html>
document.getElementById('popupButton').addEventListener('click', function() {
alert('这是一个弹窗!');
});
popupButton
。script.js
。document.getElementById
获取按钮元素。alert
方法显示一个简单的弹窗,内容为“这是一个弹窗!”。alert
方法是最基本的弹窗方式,易于理解和实现。alert
方法。confirm
方法,可以获取用户的确认或取消操作。alert
方法的样式由浏览器决定,无法自定义。如果你需要更复杂的弹窗效果,可以使用以下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Popup Example</title>
<style>
.popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<button id="popupButton">点击我</button>
<div class="overlay" id="overlay"></div>
<div class="popup" id="popup">
<p>这是一个自定义弹窗!</p>
<button id="closePopup">关闭</button>
</div>
<script src="script.js"></script>
</body>
</html>
document.getElementById('popupButton').addEventListener('click', function() {
document.getElementById('popup').style.display = 'block';
document.getElementById('overlay').style.display = 'block';
});
document.getElementById('closePopup').addEventListener('click', function() {
document.getElementById('popup').style.display = 'none';
document.getElementById('overlay').style.display = 'none';
});
这个示例展示了如何创建一个自定义的弹窗,并通过点击按钮显示和隐藏弹窗。
领取专属 10元无门槛券
手把手带您无忧上云