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 分钟前
告别表单“黄油色”:如何优雅地重置 Chrome 自动填充样式
前端
Dragon Wu4 分钟前
Taro Webpack 5 编译过慢的解决方案
前端·webpack·小程序·taro
认真学GIS16 分钟前
日尺度地下水水位!全国11897个地下水动态监测站点2005-2021年日尺度地下水水位(地下水埋深)(EXCEL格式)数据
服务器·前端·excel
_DoubleL17 分钟前
Volta启动项目自动切换Node版本
前端·node.js
阿里巴巴终端技术24 分钟前
[第 20 届 D2 倒计时] 7 大专场演讲、44 个精彩话题、D2 之夜畅聊 AI + 终端的发展前景
前端·人工智能·程序员
进击的雷神28 分钟前
前端路由动态渲染、JSON内嵌HTML清洗、展位信息数组化、分页参数固定化——尼日利亚展会爬虫四大技术难关攻克纪实
前端·爬虫·python·json
文心快码BaiduComate35 分钟前
Comate 4.0的自我进化:后端“0帧起手”写前端、自己修自己!
前端·后端·架构
cipher36 分钟前
Web3全栈学习与实战项目
前端·后端·区块链
冴羽38 分钟前
资深前端都在用的 9 个调试偏方
前端·javascript
兆子龙1 小时前
xx.d.ts 文件有什么用,为什么不引入都能生效?
javascript