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>
相关推荐
网络点点滴10 分钟前
Vue3路由params参数
前端·javascript·vue.js
hqk13 分钟前
鸿蒙 ArkUI 从零到精通:基础语法全解析
android·前端·harmonyos
wordbaby40 分钟前
React Native (Expo) iOS 真机调试失败排查:xcodebuild exited with error code 65
前端·react native
今天也很困1 小时前
解决浏览器后台定时器降频问题:用 Worker 实现高精度 setInterval
前端
只与明月听1 小时前
一次uniapp问题排查
前端·javascript·vue.js
Bacon1 小时前
Vitest 一个基于 Vite 的快速单元测试框架
前端
学不动学不明白1 小时前
AES-GCM 解密失败解决方案
前端
一水鉴天1 小时前
整体设计 定稿 之16 三层智能合约体系实现设计和开发的实时融合
前端·人工智能·架构·智能合约
捕捉一只前端小白1 小时前
前端面试题(仅供参考)
html·css3·js
小菜今天没吃饱1 小时前
DVWA-XSS(Reflected)
前端·xss·dvwa