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>
相关推荐
opbr几秒前
还在手写 env 类型定义?这个 Vite 插件帮你自动搞定!
前端·vite
Qinana几秒前
前端正则表达式全解:从基础语法到实战应用
前端·javascript·面试
蜡台4 分钟前
JavaScript Object Function ERROR
开发语言·javascript·ecmascript·error
烟话66 分钟前
vue3响应式基础
前端·javascript·vue.js
boombb9 分钟前
用户反馈入口
前端
im_AMBER10 分钟前
万字长文:手撕JS深浅拷贝完全指南
前端·javascript·面试
还是大剑师兰特12 分钟前
pinia-plugin-persistedstate详解与Vue3使用示例
开发语言·javascript·ecmascript
@大迁世界16 分钟前
20.“可复用组件”具体指的是什么?如何设计与产出这类组件?.
开发语言·前端·javascript·ecmascript
Bigger19 分钟前
第二章:我是如何剖析 Claude Code QueryEngine 与大模型交互机制的
前端·ai编程·源码阅读
FelixBitSoul23 分钟前
彻底吃透 React Hook:它背后的执行模型到底是什么? 🚀
前端