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>
相关推荐
777VG3 小时前
PostgreSQL +martin将多张表输出成一个 MVT
前端·数据库·postgresql
mayaairi3 小时前
JS循环语句深度解析:嵌套for、while与do...while
开发语言·前端·javascript
To_OC3 小时前
啃完流式输出:从一个卡顿的 LLM 接口开始,我搞懂了数据流到底怎么 “流”
前端·javascript·llm
阳光是sunny4 小时前
LangGraph实战教程:defer延迟节点——让收尾工作自动排到最后
前端·人工智能·后端
kyriewen4 小时前
我用了三周Claude Code Skills——总结出5条铁律,第3条最反直觉
前端·ai编程·claude
阳光是sunny4 小时前
LangGraph实战教程:控制流详解
前端·人工智能·后端
格尔曼Noah5 小时前
Safari浏览器中如何只允许指定网站下载
前端·safari
用户059540174466 小时前
用了3年Redis,才发现我一直没搞懂缓存一致性测试
前端·css
swipe6 小时前
07|(前端转后全栈)为什么后端也要缓存?从前端缓存思维理解 Redis
前端·后端·全栈
IT小盘6 小时前
05-企业项目统一接入多个大模型-适配器模式实战
前端·人工智能·适配器模式