首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用JavaScript显示随机数列表并将其显示在Modal上

的方法如下:

  1. 首先,创建一个HTML页面,包含一个按钮和一个Modal(弹出框)元素。给按钮添加一个点击事件,当点击按钮时,弹出Modal。
代码语言:txt
复制
<!DOCTYPE html>
<html>
<head>
    <title>随机数列表</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>
    <button onclick="openModal()">显示随机数列表</button>

    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <h2>随机数列表</h2>
            <ul id="randomList"></ul>
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>
  1. 在JavaScript文件(script.js)中,编写生成随机数列表、打开和关闭Modal的函数。
代码语言:txt
复制
function openModal() {
    var modal = document.getElementById("myModal");
    modal.style.display = "block";

    generateRandomList();
}

function closeModal() {
    var modal = document.getElementById("myModal");
    modal.style.display = "none";
}

function generateRandomList() {
    var randomList = document.getElementById("randomList");
    randomList.innerHTML = "";

    for (var i = 0; i < 10; i++) {
        var randomNumber = Math.floor(Math.random() * 100);
        var listItem = document.createElement("li");
        listItem.textContent = randomNumber;
        randomList.appendChild(listItem);
    }
}
  1. 运行HTML页面,点击按钮即可显示随机数列表的Modal。

这个例子中,我们使用了JavaScript来生成随机数列表,并将其显示在Modal上。当点击按钮时,Modal弹出并显示随机数列表,点击Modal上的关闭按钮或背景区域,Modal关闭。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券