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>
相关推荐
骐骥11 天前
鸿蒙开发使用DevTools工具调试ArkWeb组件中的前端页面
前端·harmonyos·调试·arkweb·纯鸿蒙
WHOVENLY1 天前
【javaScript】- 笔试题合集(长期更新,建议收藏,目前已更新至31题)
开发语言·前端·javascript
指尖跳动的光1 天前
将多次提交合并成一次提交
前端·javascript
程序员码歌1 天前
短思考第263天,每天复盘10分钟,胜过盲目努力一整年
android·前端·后端
oden1 天前
1 小时速通!手把手教你从零搭建 Astro 博客并上线
前端
若梦plus1 天前
JS之类型化数组
前端·javascript
若梦plus1 天前
Canvas 深入解析:从基础到实战
前端·javascript
若梦plus1 天前
Canvas渲染原理与浏览器图形管线
前端·javascript
C_心欲无痕1 天前
vue3 - 依赖注入(provide/inject)组件跨层级通信的优雅方案
前端·javascript·vue.js