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>
相关推荐
三十而立洋8 分钟前
彻底搞懂跨域:原理、CORS、解决方案与实战避坑
前端
xiaominlaopodaren8 分钟前
three.js最小地图运行时(十):接入离线逻辑瓦片剖面
javascript·gis·three.js
计算机魔术师10 分钟前
ASR转录总出错?Google新模型WER降到2.6%,还能自动帮你改口误
前端
xcs1940511 分钟前
新版 IDEA(尤其是 2024/2025/2026)越来越臃肿
前端·人工智能·intellij-idea
Patrick_Wilson12 分钟前
iOS 第三方浏览器图片下载失败问题
前端·ios·浏览器
菜鸟小前端在线卖艺17 分钟前
因为找不到好用的前端占位图,于是我自己写了个谁都能用的占位图功能
前端·程序员·产品
Z小明18 分钟前
第 1 章 Vite 项目初始化
前端·vue.js
IT_陈寒20 分钟前
Redis大KEY删除慢到手抖,这几个方法让我少熬一夜
前端·人工智能·后端
AICoder码农王22 分钟前
depcruise 实战:把架构约定变成可执行的检查
前端
paopaokaka_luck22 分钟前
基于springboot3+vue3+uniapp的河南非遗数字图谱小程序(协同过滤算法、数字图谱展示、ECharts 图形化分析)
java·前端·spring boot·学习·小程序·uni-app·echarts