HTML 页面中点击按钮实现跳转,有几种常用方法,这里给你最简单、常用的示例:
方法一:使用 <button>
+ JavaScript 的 onclick
<button onclick="window.location.href='https://www.example.com'">
点击跳转到示例网站
</button>
点击按钮时,通过 JavaScript 修改 window.location.href
实现页面跳转。
方法二:使用 <a>
标签包裹按钮样式
<a href="https://www.example.com">
<button>点击跳转到示例网站</button>
</a>
浏览器会将 <button>
当作普通元素包裹在 <a>
里,点击时直接跳转。
方法三:纯 CSS 样式的链接按钮
<a href="https://www.example.com" style="
display: inline-block;
padding: 10px 20px;
background-color: #3498db;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 16px;
">
点击跳转到示例网站
</a>
用 <a>
标签并加按钮样式,避免使用 <button>
,语义更准确。
额外说明:
- 如果按钮在表单中,避免用
<button type="submit">
,否则会提交表单。 - JavaScript 的跳转方式可灵活控制,比如条件跳转、延迟跳转等。
需要我帮你写完整的示例文件吗?或者演示点击按钮跳转到页面内某个锚点?
发表回复