当前位置: 首页 > 建站教程

javascript怎么使用alert实现一个精美的弹窗

时间:2026-01-31 10:53:34

要实现一个精美的弹窗,你可以使用CSS样式来美化alert弹窗的外观。以下是一个示例代码:

<!DOCTYPE html><html><head><style>.alert {position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);background-color: #fff;border: 1px solid #ccc;border-radius: 5px;padding: 20px;box-shadow: 0 0 10px rgba(0,0,0,0.1);}.alert h2 {margin: 0;font-size: 24px;color: #333;}.alert p {margin: 10px 0;font-size: 16px;color: #666;}.alert button {padding: 10px 20px;background-color: #007bff;color: #fff;border: none;border-radius: 5px;cursor: pointer;}.alert button:hover {background-color: #0056b3;}</style></head><body><button onclick="showAlert()">Show Alert</button><script>function showAlert() {var modal = document.createElement('div');modal.className = 'alert';modal.innerHTML = '<h2>Welcome!</h2><p>This is a beautiful alert box.</p><button onclick="closeAlert()">Close</button>';document.body.appendChild(modal);}function closeAlert() {document.querySelector('.alert').remove();}</script></body></html>

在上面的示例中,我们定义了一个样式为alert的弹窗,并通过JavaScript动态创建了这个弹窗。点击按钮时会显示这个弹窗,点击弹窗上的按钮可以关闭弹窗。你可以根据需求修改样式和内容,实现更加精美的弹窗效果。


上一篇:Spring之TransactionProxyFactoryBean怎么用
下一篇:C#控制鼠标操作怎么实现
JavaScript
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素