html如何实现按钮跳转,以及访问随机跳转

html如何实现按钮跳转,以及访问随机跳转。

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>访问者跳转模拟</title>
    <script type="text/javascript">
        function redirectToPort() {
            // 基于时间或随机数生成端口号
            var basePort = 800; // 起始端口号
            var maxPort = 802; // 最大端口号
            var date = new Date();
            var seconds = date.getSeconds(); // 获取当前秒数
            var portOffset = seconds % (maxPort - basePort + 1); // 计算偏移量
            var targetPort = basePort + portOffset; // 计算目标端口号

            // 构建目标URL
            var targetUrl = "http://192.168.16.185:" + targetPort;

            // 跳转到目标URL
            window.location.href = targetUrl;
        }
    </script>
</head>
<body onload="redirectToPort();">
    <p>正在根据访问顺序跳转,请稍候...</p>
</body>
</html>

如果是按钮跳转的话

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>选择跳转端口</title>
</head>
<body>
    <h1>请选择您要跳转的端口:</h1>
    <button onclick="redirectToPort(800)">跳转到端口 800</button>
    <button onclick="redirectToPort(801)">跳转到端口 801</button>
    <button onclick="redirectToPort(802)">跳转到端口 802</button>

    <script>
        function redirectToPort(port) {
            const targetUrl = `http://192.168.16.185:${port}`;
            window.location.href = targetUrl;
        }
    </script>
</body>
</html>

需求看似很冷门,实际上这个对应我上一篇文章

相关推荐
lecepin16 分钟前
AI Coding 资讯 2025-09-17
前端·javascript·面试
IT_陈寒18 分钟前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端
树上有只程序猿1 小时前
终于有人把数据库讲明白了
前端
猩兵哥哥1 小时前
前端面向对象设计原则运用 - 策略模式
前端·javascript·vue.js
司宸1 小时前
Prompt设计实战指南:三大模板与进阶技巧
前端
RoyLin1 小时前
TypeScript设计模式:抽象工厂模式
前端·后端·typescript
华仔啊1 小时前
Vue3+CSS 实现的 3D 卡片动画,让你的网页瞬间高大上
前端·css
江城开朗的豌豆1 小时前
解密React虚拟DOM:我的高效渲染秘诀 🚀
前端·javascript·react.js
vivo互联网技术1 小时前
拥抱新一代 Web 3D 引擎,Three.js 项目快速升级 Galacean 指南
前端·three.js