当前位置:网站首页>你的 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边栏推荐
- TF中使用zeros(),ones(), fill()方法生成数据
- In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
- 面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
- SecureCRT background color
- BulkInsert方法实现批量导入
- Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
- 为什么这么多人都想当产品经理?
- APP automation test framework - UiAutomator2 introductory
- mysql 找不到或无法加载已注册的 .Net Framework Data Provider。
- laravel table migration error [easy to understand]
猜你喜欢

Synchronization lock synchronized traces the source

任务流执行器是如何工作的?

数独 | 回溯-7

MLOps的演进历程

Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)

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

Space not freed after TRUNCATE table

为什么这么多人都想当产品经理?

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
随机推荐
APP自动化测试框架-UiAutomator2基础入门
国内手机厂商曾为它大打出手,如今它却最先垮台……
PHP 2D array sorted by a field
一本通2074:【21CSPJ普及组】分糖果(candy)
【微服务~Nacos】Nacos之配置中心
AI识万物:从0搭建和部署手语识别系统
Flutter 绘制美不胜收的光影流动效果
np中的round函数,ceil函数与floor函数
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
Solution: Edu Codeforces 109 (div2)
Interviewer: How to deal with Redis big key?
蔚来杯2022牛客暑期多校训练营7 CFGJ
Converting angles to radians
TF generates uniformly distributed tensor
宝塔实测-搭建LightPicture开源图床系统
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
APP automation test framework - UiAutomator2 introductory
Leetcode 93 IP addresses
random.normal() and random.truncated_normal() in TF