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>
相关推荐
Damai20 分钟前
DOMPurify 完整介绍
前端·面试
随风一样自由22 分钟前
【前端+跨域】跨域问题全面解析与解决方案指南
前端·跨域
小帅不太帅36 分钟前
我随手发了几张截图,白拿了一个月 AI 顶配会员
前端·github
36岁的我开始学习打篮球36 分钟前
外规内化技术架构
前端
iCOD3R1 小时前
【趣站精选】2026-07-13
开源·html
雪隐1 小时前
用Flutter做背单词APP-03为了给单词库塞满数据,我让AI给我打了上万份工
前端·人工智能·后端
hunterandroid1 小时前
Android 安全最佳实践:从数据存储到网络通信的防护思路
前端
H Journey1 小时前
web开发学习:html、css、js
前端·css·html·js
hunterandroid1 小时前
Compose 性能优化:从卡顿到丝滑的实战经验
前端
hunterandroid1 小时前
Kotlin Coroutines 在 Android 项目中的落地实战
前端