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>
相关推荐
宁雨桥14 小时前
前端设计模式面试题大全
前端·设计模式
Cg1362691597414 小时前
JS函数表示
前端·html
℘团子এ14 小时前
vue3中,el-table表格固定列后出现表格线段折断的问题
javascript·vue.js·elementui
在屏幕前出油14 小时前
02. FastAPI——路由
服务器·前端·后端·python·pycharm·fastapi
勿芮介15 小时前
【大模型应用】在window/linux上卸载OpenClaw
java·服务器·前端
摸鱼仙人~15 小时前
前端面试手写核心 Cheat Sheet(终极精简版)
前端
馬致远15 小时前
Win7 配置 Vue脚手架
javascript·vue.js·ecmascript
Ashley_Amanda15 小时前
深入浅出Web Dynpro:SAP企业级Web应用开发全面解析
前端
方安乐15 小时前
概念:前端工程化实践
前端