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>
相关推荐
小蜜蜂dry4 分钟前
nestjs学习 - 控制器、提供者、模块
前端·node.js·nestjs
优秀稳妥的JiaJi5 分钟前
基于腾讯地图实现电子围栏绘制与校验
前端·vue.js·前端框架
前端开发呀29 分钟前
从 qiankun(乾坤) 迁移到 Module Federation(模块联邦),对MF只能说相见恨晚!
前端
Lee川32 分钟前
深度解构JavaScript:作用域链与闭包的内存全景图
javascript·面试
没想好d37 分钟前
通用管理后台组件库-10-表单组件
前端
恋猫de小郭42 分钟前
你用的 Claude 可能是虚假 Claude ,论文数据告诉你,Shadow API 中的欺骗性模型声明
前端·人工智能·ai编程
_Eleven1 小时前
Pinia vs Vuex 深度解析与完整实战指南
前端·javascript·vue.js
cipher1 小时前
HAPI + 设备指纹认证:打造更安全的远程编程体验
前端·后端·ai编程
技术狂小子1 小时前
# 一个 Binder 通信中的多线程同步问题
javascript·vue.js
WeNTaO2 小时前
ACE Engine FrameNode 节点
前端