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>
相关推荐
之歆1 小时前
DAY04_HTML&CSS_从表单到样式的完整学习指南
css·html·产品运营
\xin2 小时前
pikachu自编exp,xss反射性get,post,存储型xss,dom,dom-x
前端·javascript·xss
是烟花哈7 小时前
【前端】React框架学习
前端·学习·react.js
qq4356947017 小时前
JavaWeb08
前端
2401_878454538 小时前
html和css的复习(1)
前端·css·html
@PHARAOH8 小时前
WHAT - git worktree 概念
前端·git
冰的第三次元9 小时前
一天通关HTML80%核心细节(新手友好版)
html
IT_陈寒9 小时前
我竟然被JavaScript的隐式类型转换坑了三天!
前端·人工智能·后端
我亚索贼六丶9 小时前
二十六. AI基础概念之如何更好的使用AI
前端
小码哥_常9 小时前
安卓启动页Logo适配秘籍:告别“奇形怪状”的展示
前端