当前位置:网站首页>你的 Link Button 能让用户选择新页面打开吗?
你的 Link Button 能让用户选择新页面打开吗?
2022-08-09 21:54:00 【InfoQ】
1. 什么是Link Button?
什么是导航能力?
<a>href<button>hrefonclick2. 用户怎么选择新页面打开?
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>onclickwindow.open(url)window.document.href = urlwindow.location.replace(URL)- 用户根本无法选择在新页面or本页面打开,只能接受你的实现。
- 用户根本不知道点击按钮后会发生什么。(如果是
<a>标签,用户hover时,会在浏览器左下方看到新页面 URL)
4.2 中手方案:
<button>
+
onclick
+
event
CommandCtrl// buttonElement 是html中某个<button>元素
buttonElement.onclick = function (event) {
if (event.ctrlKey || event.metaKey) {
window.open('某个url');
} else {
window.document.href = '某个url';
}
};
onclickeventCtrlCommand4.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. 写在最后
MenuButtonLinkLink边栏推荐
- Ehrlich screening method: Counting the number of prime numbers
- Pagoda measurement - building LightPicture open source map bed system
- Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool
- Evolution of MLOps
- AI识万物:从0搭建和部署手语识别系统
- abstract class or interface
- Deceptive Dice
- 上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
- In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
- 【EF】数据表全部字段更新与部分字段更新
猜你喜欢

AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero

Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert

你真的了解乐观锁和悲观锁吗?

nvm下node安装;node环境变量配置

leetcode 刷题日记 计算右侧小于当前元素的个数

Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool

TF generates uniformly distributed tensor

在“企业通讯录”的盲区,融云的边界与分寸

金山云地震,震源在字节?

The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
随机推荐
重装系统后新建文本文档打不开怎么办
【测试】语句覆盖,判定覆盖,条件覆盖,路径覆盖
JS解混淆-AST还原案例
Bean生命周期
random.normal() and random.truncated_normal() in TF
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
【stack】【queue】【priority_queue】【deque】Detailed explanation
好未来,想成为第二个新东方
Sudoku | Backtrack-7
重要的不是成为海贼王,而是像路飞一样去冒险
2022年中国第三方证券APP创新专题分析
6个规则去净化你的代码
L3-2 Delete up to three characters (30 points)
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
基于ABP的AppUser对象扩展
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
Flask入门学习教程
Flutter 绘制美不胜收的光影流动效果
TF中使用zeros(),ones(), fill()方法生成数据
蔚来杯2022牛客暑期多校训练营7 CFGJ