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>
相关推荐
Codebee12 分钟前
Harness Engineering:AICode 的灵魂
前端·人工智能·前端框架
Highcharts.js29 分钟前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图
hpysirius31 分钟前
在企业搭建一套完整的AI Agent系统
前端
追逐梦想永不停32 分钟前
记录一个好用的excel判断数字格式的公式
前端·chrome·excel
hpysirius38 分钟前
从零构建 Web 端视频剪辑器:技术实践与思考
前端
让学习成为一种生活方式40 分钟前
大肠杆菌合成扑热息痛--对乙酰氨基酚--文献精读227
开发语言·前端·javascript
李白的天不白1 小时前
请求不到百度网址的原因
前端
多秋浮沉度华年1 小时前
electron 初始使用记录
javascript·arcgis·electron
Gary Studio1 小时前
Selinux编写
linux·服务器·前端
网络点点滴1 小时前
NPM的包版本管理
前端·npm·node.js