Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

html弹窗

作者:靈魂风尖上   发布日期:2026-02-08   浏览:83

<!DOCTYPE html>
<html>
<head>
    <title>HTML 弹窗示例</title>
    <style>
        /* 设置弹窗的样式 */
        .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;
        }
    </style>
</head>
<body>

<h2>点击按钮显示弹窗</h2>

<!-- 触发弹窗的按钮 -->
<button id="myBtn">打开弹窗</button>

<!-- 弹窗内容 -->
<div id="myModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <p>这是一个简单的 HTML 弹窗示例。</p>
    </div>
</div>

<script>
    // 获取弹窗元素
    var modal = document.getElementById("myModal");

    // 获取触发弹窗的按钮元素
    var btn = document.getElementById("myBtn");

    // 获取关闭按钮元素
    var span = document.getElementsByClassName("close")[0];

    // 当用户点击按钮时,显示弹窗
    btn.onclick = function() {
        modal.style.display = "block";
    }

    // 当用户点击关闭按钮时,隐藏弹窗
    span.onclick = function() {
        modal.style.display = "none";
    }

    // 当用户点击弹窗外部区域时,隐藏弹窗
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
</script>

</body>
</html>

解释说明:

  1. HTML 结构:

    • 包含一个按钮 (<button>) 和一个弹窗 (<div>),弹窗中有一个关闭按钮 (<span>) 和一段文本。
  2. CSS 样式:

    • .modal 类用于定义弹窗的样式,默认是隐藏的 (display: none)。
    • .modal-content 类用于定义弹窗内容的样式。
    • .close 类用于定义关闭按钮的样式,并设置鼠标悬停时的效果。
  3. JavaScript 功能:

    • btn.onclick:当用户点击按钮时,显示弹窗。
    • span.onclick:当用户点击关闭按钮时,隐藏弹窗。
    • window.onclick:当用户点击弹窗外部区域时,隐藏弹窗。

上一篇:html border

下一篇:注册页面html代码

大家都在看

静态html源码

404 html

ios打开html

nginx访问不到html

html 符号

colspan在html中是什么意思

怎么将网页另存为html

xml转html

html时间代码

html localstorage

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站