当前位置:网站首页>你的 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
边栏推荐
- 技术分享 | 接口自动化测试如何处理 Header cookie
- js十五道面试题(含答案)
- LeetCode26: remove duplicates in sorted array
- Let's talk about what DDL, DML, DQL and DCL are in SQL statements
- Js fifteen interview questions (with answers)
- Basic operations of openGauss database (super detailed)
- Under the NVM node installation;The node environment variable configuration
- L3-2 Delete up to three characters (30 points)
- UML类图五种关系的代码实现[通俗易懂]
- Cookie、session、token
猜你喜欢
One Pass 2074: [21CSPJ Popularization Group] Candy
js十五道面试题(含答案)
Js fifteen interview questions (with answers)
TF generates uniformly distributed tensor
第十七期八股文巴拉巴拉说(数据库篇)
好未来,想成为第二个新东方
孙正义亏掉1500亿:当初投贵了
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview
从产品角度看 L2 应用:为什么说这是一个游乐场?
APP automation test framework - UiAutomator2 introductory
随机推荐
Simple questions peek into mathematics
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
万字总结:分布式系统的38个知识点
Use zeros(), ones(), fill() methods to generate data in TF
【EF】数据表全部字段更新与部分字段更新
LeetCode26:删除有序数组中的重复项
Pagoda measurement - building LightPicture open source map bed system
TRUNCATE表之后空间未释放
台风生成,广州公交站场积极开展台风防御安全隐患排查
Converting angles to radians
Use convert_to_tensor in Tensorflow to specify the type of data
【双链表增删查改接口的实现】
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
Bean life cycle
4D Summary: 38 Knowledge Points of Distributed Systems
Flask入门学习教程
[Microservice~Nacos] Nacos service provider and service consumer
宝塔实测-搭建LightPicture开源图床系统
MLOps的演进历程
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert