当前位置:网站首页>你的 Link Button 能让用户选择新页面打开吗?
你的 Link Button 能让用户选择新页面打开吗?
2022-08-09 21:54:00 【InfoQ】
1. 什么是Link Button?
什么是导航能力?
<a>
href
<button>
href
onclick
2. 用户怎么选择新页面打开?
2.1 新标签页(tab)打开
Command
(Mac)/Ctrl
(Windows) + 鼠标左键click
- 鼠标中键click
- 鼠标右键click,在菜单选择“在新标签页中打开链接”
- (无障碍)通过
Tab
,选中链接时,按Command
(Mac)/Ctrl
(Windows) + 回车键Enter
2.2 新窗口(window)打开
Shift
+ 鼠标左键click
- 鼠标右键click,在菜单选择“在窗口中打开链接”
- (无障碍)通过
Tab
,选中链接时,按Shift
+ 回车键Enter
3. 什么是极致的用户体验?
4. 如何优雅的实现“Link Button”
4.1 新手方案:
<button>
+
onclick
<button>
onclick
window.open(url)
window.document.href = url
window.location.replace(URL)
- 用户根本无法选择在新页面or本页面打开,只能接受你的实现。
- 用户根本不知道点击按钮后会发生什么。(如果是
<a>
标签,用户hover时,会在浏览器左下方看到新页面 URL)
4.2 中手方案:
<button>
+
onclick
+
event
Command
Ctrl
// buttonElement 是html中某个<button>元素
buttonElement.onclick = function (event) {
if (event.ctrlKey || event.metaKey) {
window.open('某个url');
} else {
window.document.href = '某个url';
}
};
onclick
event
Ctrl
Command
4.3 高手方案:
<a>
+
onclick
+
event
<a>
<a>
4.3.1 样式问题
<button>
<a>
<a>
<a>
a, a:link, a:visited, a:hover, a:active {
color: inherit;
text-decoration: none;
}
4.3.2 JS逻辑问题
<a>
href
- 跳转时,需要传路由state。
- 某些逻辑,只希望本页面跳转时执行,不允许新页面打开时执行(因为JS只能执行本页面的JS,如果在新页面打开,本页面应该保持不变,不能执行那段JS,例如React Router中的
<Link>
)。
- 某个按钮,直接点击时是
window.history.back()
,但也允许新窗口打开上个页面地址(这个问题更加复杂,请期待我的下篇文章,会做详细讲解)
这些问题的解决方案
// aElement是html中的某个包含href的<a>元素: <a href="某个url">某个链接</a>
aElement.onclick = function (event) {
if (event.button !== 0) return;
if (event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
event.preventDefault();
// 如果用户期望本页面跳转(而非新窗口打开),则执行以下逻辑
window.history.pushState({ pageState: 123 }, '', 'new-page.html');
};
event.button
- 0:主按键,通常指鼠标左键或默认值
- 1:辅助按键,通常指鼠标滚轮中键
- 2:次按键,通常指鼠标右键
- 3:第四个按钮,通常指浏览器后退按钮
- 4:第五个按钮,通常指浏览器的前进按钮
event.xxxKey
event.ctrlKey
: MAC上表示Control键,Windows上表示Ctrl键。
event.metaKey
: MAC上表示Command键,Windows上表示Windows键。
event.shiftKey
: Shift键。
event.altKey
: MAC上表示Option键,Windows上表示Alt键。
ctrlKey
+click
: Mac上表示右键点击该元素,Windows上表示新标签页打开页面。
metaKey
+click
: Mac上表示新标签页打开页面,Windows上打开Windows开始菜单。
shiftKey
+click
: 新窗口打开页面。
altKey
+click
: 下载页面。
event.preventDefault()
<a>
5. 写在最后
Menu
Button
Link
Link
边栏推荐
- 【EF】数据表全部字段更新与部分字段更新
- 论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
- 【双链表增删查改接口的实现】
- SQLi-LABS Page-2 (Adv Injections)
- Pagoda measurement - building LightPicture open source map bed system
- CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
- [Microservice~Nacos] Nacos service provider and service consumer
- This article lets you quickly understand implicit type conversion [integral promotion]!
- MLOps的演进历程
- TF使用constant生成数据
猜你喜欢
APP automation test framework - UiAutomator2 introductory
【微服务~Nacos】Nacos之配置中心
6 rules to sanitize your code
简单问题窥见数学
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview
STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
Install win virtual machine on VMware
Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
Evolution of MLOps
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
随机推荐
Basic JSON usage
从产品角度看 L2 应用:为什么说这是一个游乐场?
JSON 基本使用
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
孙正义亏掉1500亿:当初投贵了
Interviewer: How to deal with Redis big key?
Metasploit常用命令、技术功能模块
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
Tensorflow中使用convert_to_tensor去指定数据的类型
技术分享 | 接口自动化测试如何处理 Header cookie
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
Synchronization lock synchronized traces the source
Converting angles to radians
laravel table migration error [easy to understand]
Simple questions peek into mathematics
Tensorflow模型整体构建流程
Ehrlich screening method: Counting the number of prime numbers
leetcode 刷题日记 计算右侧小于当前元素的个数
国内手机厂商曾为它大打出手,如今它却最先垮台……