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>
相关推荐
江公望2 分钟前
Vue3的 nextTick API 5分钟讲清楚
前端·javascript·vue.js
weixin_446260853 分钟前
深入了解 MDN Web Docs:打造更好的互联网
前端
Codebee10 分钟前
# 🔥A2UI封神!元数据驱动的AI交互新范式,技术人必看
前端·架构
JarvanMo18 分钟前
展望 2030 年:移动开发者的未来将如何?
前端
我的xiaodoujiao19 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 34--基础知识 9--文件上传功能
前端·python·测试工具·ui·pytest
辛-夷20 分钟前
pinia与Vuex高频面试题
前端·vue.js
咸鱼加辣22 分钟前
【python面试】Python 的 lambda
javascript·python·算法
.生产的驴28 分钟前
泛微E10二开 前端Ecode拦截器
前端
亿元程序员34 分钟前
PinK(Cocos4.0?)生成飞机大战,抢先体验全流程!
前端
晓得迷路了38 分钟前
栗子前端技术周刊第 111 期 - Next.js 16.1、pnpm 10.26、Bun 1.3.5...
前端·javascript·bun