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>
相关推荐
boooooooom2 小时前
手把手做一个图 RAG 烹饪问答系统:Neo4j + Milvus + LLM 的工程实践
前端·javascript·后端
小小善后师2 小时前
HID 设备对接技术解析:基于本地中间服务的 WebSocket 通信模式
前端
黄油面包2 小时前
Codex 额度三天见底后,我重新做了一周预算
前端·人工智能
PedroQue992 小时前
v2.7.1:修复 H5 端返回死循环闪烁问题
前端·uni-app
coderCN2 小时前
Nodejs express+knex(ORM框架)
前端·node.js
求道於盲2 小时前
python中的抽象类
前端
Csvn2 小时前
CSS 层叠与现代布局:BFC、@layer 与 grid/flex 的取舍
前端
CodeSheep3 小时前
OpenJDK 全面禁止 AI 生成代码!
前端·后端·程序员
IT_陈寒3 小时前
为什么我的Vue组件总是莫名其妙重渲染?
前端·人工智能·后端
乘风gg3 小时前
企业级 AI Coding 的 Harness 工程实战:8 个 Skill 串起全链路
前端·ai编程·claude