要给 HTML 访问添加密码功能,可以使用 JavaScript 编写以下代码:

<!DOCTYPE html>
<html>
<head>
    <title>Protected Page</title>
    <script>
        function checkPassword() {
            var password = document.getElementById('password').value;
            if (password === 'mypassword') {
                window.location.href = 'protected.html'; // 跳转到受保护的页面
            } else {
                alert('密码错误,请重试!');
            }
        }
    </script>
</head>
<body>
    <h1>请输入密码访问受保护的页面</h1>
    <input type="password" id="password">
    <button onclick="checkPassword()">提交</button>
</body>
</html>

在上述代码中,我们创建了一个输入密码的文本框和一个提交按钮。当用户点击提交按钮时,会调用 checkPassword() 函数。该函数会获取用户输入的密码并与预设的密码进行比较。如果密码正确,将会跳转到 protected.html 页面;如果密码错误,将会弹出一个提示框。你可以将上述代码保存为一个 HTML 文件,然后将 protected.html 替换为你想要保护的页面的 URL。请记得将预设的密码 mypassword 修改为你自己的密码。

HTML 页面密码保护 - 简单代码实现

原文地址: http://www.cveoy.top/t/topic/pbXF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录