2023.11.18html中如何使用input/button进行网页跳转

2023.11.18html中如何使用input/button进行网页跳转

在做网页时有时会用<button>元素,有时会用<input>元素进行form表单操作或者网页跳转,但是用bootstrap时两种元素会出现不同的样式,为了样式一致,有时需要使用这两种元素相互实现其常用功能。

使用<button>元素进行网页跳转:

复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Button Redirect</title>
    <script>
        function redirectToPage() {
            window.location.href = "https://www.example.com";
        }
    </script>
</head>
<body>
    <button onclick="redirectToPage()">跳转到 Example 网站</button>
</body>
</html>

使用js调用redirectToPage()函数,该函数使用window.location.href实现网页跳转。

使用<input>实现网页跳转。可以使用<input>元素的type="button"属性创建一个按钮,并使用JavaScript的onclick事件来执行跳转操作:

复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Input Redirect</title>
    <script>
        function redirectToPage() {
            window.location.href = "https://www.example.com";
        }
    </script>
</head>
<body>
    <input type="button" value="跳转到 Example 网站" onclick="redirectToPage()">
</body>
</html>
相关推荐
bluceli9 小时前
CSS容器查询:响应式设计的新范式
前端·css
Tapir9 小时前
被 Karpathy 下场推荐的 NanoClaw 是什么来头
前端·后端·github
前端人类学9 小时前
深入解析JavaScript中的null与undefined:区别、用法及判断技巧
前端·javascript
ssshooter11 小时前
Tauri 项目实践:客户端与 Web 端的授权登录实现方案
前端·后端·rust
兆子龙11 小时前
【React】19 深度解析:掌握新一代 React 特性
前端·架构
Moment11 小时前
MinIO已死,MinIO万岁
前端·后端·github
无双_Joney11 小时前
心路散文 - 转职遇到AI浪潮,AIGC时刻人的价值是什么?
前端·后端·架构
有意义11 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
小怪点点12 小时前
vue3使用
前端·vue.js
进击的尘埃12 小时前
Vitest 自定义 Reporter 与覆盖率卡口:在 Monorepo 里搞增量覆盖率检测
javascript