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>
相关推荐
llz_112几秒前
web-第六次课后作业
前端·spring boot·后端
zzqssliu4 分钟前
基于Laravel + Express.js的代购系统多语言多货币架构设计
javascript·express·laravel
爱勇宝9 分钟前
CEO通知5100名员工:今年不涨薪了,钱要投给AI!
前端·后端·程序员
乘风gg18 分钟前
前端死到第几轮了?得物前端部门解散有感!
前端·ai编程·claude
艾伦野鸽ggg22 分钟前
web 组大一下第二次考核
前端·css·html
水煮白菜王28 分钟前
高德地图"未获得商用授权"水印临时移除方案
前端·javascript
库拉AI小李29 分钟前
# 数据清洗与分析:Gemini 3.5 处理 Excel 数据的实操体验
前端·人工智能·后端
小小小小宇29 分钟前
React17 18 19 新增能力、解决问题、原理与使用详解
前端
chushiyunen30 分钟前
vue el-pagination实现分页
javascript·vue.js·elementui
by————组态31 分钟前
Ricon组态可视化编辑器 - 所见即所得的工业画布
前端·javascript·物联网·架构·编辑器·组态