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>
相关推荐
李伟_Li慢慢13 分钟前
10-mini版bindingStates
前端·three.js
এ慕ོ冬℘゜14 分钟前
深入 JavaScript BOM:掌握 window 对象的窗口控制魔法
开发语言·javascript·ecmascript
李伟_Li慢慢15 分钟前
08-BufferAttribute
前端·three.js
李伟_Li慢慢16 分钟前
09-mini版WebGLAttributes
前端·three.js
李伟_Li慢慢16 分钟前
11-mini版WebGLGeometries
前端·three.js
李伟_Li慢慢18 分钟前
18-mini版WebGLRenderList
前端·three.js
李伟_Li慢慢19 分钟前
15-mini版WebGLMaterials
前端·three.js
ji_shuke26 分钟前
Vue3 前端批量打印 PDF/图片踩坑记:跨域、合并打印、对话框闪退与按钮一直 loading
前端·pdf·状态模式·pdf打印
李伟_Li慢慢26 分钟前
06-mini版WebGLState
前端·three.js
李伟_Li慢慢30 分钟前
04-three.js的渲染流程概览
前端·three.js