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>
相关推荐
遗憾随她而去.7 小时前
Webpack5 基础篇(二)
前端·webpack·node.js
Mintopia7 小时前
🧭 一、全栈能力的重心正在从“实现” → “指令 + 验证”转移
前端·人工智能·全栈
Mintopia7 小时前
2025,我的「Vibe Coding」时刻
前端·人工智能·aigc
松涛和鸣7 小时前
DAY47 FrameBuffer
c语言·数据库·单片机·sqlite·html
西凉的悲伤8 小时前
html制作太阳系行星运行轨道演示动画
前端·javascript·html·行星运行轨道演示动画
C_心欲无痕8 小时前
网络相关 - http1.1 与 http2
前端·网络
一只爱吃糖的小羊8 小时前
Web Worker 性能优化实战:将计算密集型逻辑从主线程剥离的正确姿势
前端·性能优化
低保和光头哪个先来8 小时前
源码篇 实例方法
前端·javascript·vue.js
你真的可爱呀8 小时前
自定义颜色选择功能
开发语言·前端·javascript
小王和八蛋8 小时前
JS中 escape urlencodeComponent urlencode 区别
前端·javascript