首页 文章 精选 留言 我的

精选列表

搜索[JavaScript库],共10000篇文章
优秀的个人博客,低调大师

JavaScript 事件循环中的微任务与渲染时机:一次真实页面卡顿的排查记录

页面在持续处理 WebSocket 推送数据时出现间歇性卡顿,用 Performance 面板录制后发现,长任务被拆成了大量微任务,而关键帧渲染被推迟到了这批微任务全部清空之后。这个问题在实时数据看板、聊天界面、协作编辑这类高频更新的场景里很容易碰到:数据到达速度略高于渲染速度,代码里又习惯性用 Promise 做批处理,结果微任务队列越积越长,浏览器始终找不到机会执行样式计算和绘制。文章从一次具体的卡顿排查出发,先梳理事件循环里宏任务、微任务和渲染步骤的实际执行顺序,再讨论 Promise 链和 queueMicrotask 在批处理中的行为差异,随后给出一套让渲染有机会插入执行的可落地写法,最后补充 MutationObserver 和 requestAnimationFrame 在这类场景中的取舍。

优秀的个人博客,低调大师

别再混淆了:JavaScript 中 e.target 与 e.currentTarget 的真实区别与实战踩坑

写事件处理函数时,我见过太多人在 `click` 回调里随手用 `e.target` 去改样式或取数据,结果在嵌套结构里翻车。比如一个卡片列表,点击内部的删除按钮,却把整个卡片的背景色改了;或者用事件委托监听 `ul`,点击 `li` 里的 `span` 时拿到的 `target` 是 `span`,而你需要的是那个 `li`。这类问题的根源就是没分清 `e.target` 和 `e.currentTarget`。`target` 始终指向触发事件的原始元素,也就是你实际点中的那个节点;`currentTarget` 则指向当前绑定监听器的那个元素。在事件冒泡过程中,`target` 不会变,但 `currentTarget` 会随着事件流经过的每个监听元素而改变。这篇文章从 DOM 事件模型讲起,结合事件委托、动态元素、React 合成事件等场景,把这两个属性的行为差异和常见误用一次说清楚。 ## 从一次点击说起 假设有下面这样一段结构: ```html 标题 删除 ``` 给 `.card` 绑一个点击事件: ```js const card = document.getElementById('card'); card.addEventListener('click', function (e) { console.log('target:', e.target); console.log('currentTarget:', e.currentTarget); }); ``` 点击按钮时,控制台输出: - `target` 是 `删除` - `currentTarget` 是 `...` 点击标题时,`target` 变成 ``,`currentTarget` 还是那个 `div`。点击卡片空白处,`target` 和 `currentTarget` 才相同,都是 `div`。这个差异就是所有坑的起点。 ## 为什么 currentTarget 在异步里会变成 null 很多人写过这样的代码然后发现报错: ```js button.addEventListener('click', function (e) { setTimeout(() => { console.log(e.currentTarget); // null }, 100); }); ``` 原因是 `currentTarget` 只在事件处理函数的执行期间有值。事件派发结束后,浏览器会把 `currentTarget` 重置为 `null`。而 `target` 是真实 DOM 节点引用,不会因为事件结束而失效。所以如果你需要在异步回调里访问绑定元素,要么提前把 `e.currentTarget` 存进变量,要么直接用 `this`(非箭头函数下)或者闭包引用外部元素。 ```js button.addEventListener('click', function (e) { const btn = e.currentTarget; setTimeout(() => { btn.disabled = true; }, 500); }); ``` ## 事件委托里该用哪个 事件委托是 `currentTarget` 和 `target` 配合最典型的地方。给父容器绑定一个监听器,通过 `target` 判断实际点的是哪个子元素: ```js const list = document.getElementById('list'); list.addEventListener('click', function (e) { const item = e.target.closest('.item'); if (!item) return; if (e.target.matches('.remove-btn')) { item.remove(); } }); ``` 这里 `e.target` 可能是 `.remove-btn`,也可能是按钮里的图标 ``,所以用 `closest` 往上找最近的 `.item` 才稳妥。`e.currentTarget` 在这里始终是 `#list`,它代表委托的容器,不是被点击的业务元素。如果误把 `e.currentTarget` 当成被点击项,删除逻辑就会把整个列表删掉。 ## React 里的合成事件差异 React 17 之前,合成事件被挂载到 `document` 上,`e.currentTarget` 在事件回调里依然指向绑定元素,但 React 内部对事件对象做了池化,异步访问会出问题。React 17 之后事件挂载到 root 容器,池化也移除了,但 `currentTarget` 的语义没变:它指向当前正在执行回调的那个元素。 ```jsx function App() { const handleClick = (e) => { console.log(e.target); // 实际点击的 DOM console.log(e.currentTarget); // 绑定 onClick 的那个元素 }; return ( 点我 ); } ``` 在 React 中同样不要依赖异步里的 `e.currentTarget`,它在事件回调结束后也会被置空。需要的话先解构: ```jsx const handleClick = (e) => { const el = e.currentTarget; requestAnimationFrame(() => { el.classList.add('active'); }); }; ``` ## 一个容易忽略的坑:disabled 按钮不触发事件 当按钮被 `disabled` 时,点击它不会触发父元素的 click 事件,因为 disabled 表单控件不派发鼠标事件。这时候用事件委托去捕获删除按钮会失效。常见做法是给按钮套一层 ``,或者用 `pointer-events` 配合 `aria-disabled` 模拟禁用状态。这和 `target`/`currentTarget` 本身无关,但经常一起出现,排查时容易绕远路。 ## 该用哪个的判断标准 如果你关心的是“用户到底点中了什么”,用 `e.target`,配合 `closest`、`matches` 做过滤。如果你关心的是“哪个元素上的监听器被触发了”,用 `e.currentTarget`。在事件委托的场景里,`currentTarget` 是委托容器,`target` 是实际触发点。在直接绑定的场景里,二者通常相同,除非内部还有子元素。 写业务代码时,养成一个习惯:在回调开头先把需要的引用解构出来。 ```js function onClick(e) { const { target, currentTarget } = e; // 后续异步逻辑 safely 使用 target / currentTarget } ``` 这样既避免了异步置空的问题,也让代码意图更清晰——谁是被点的,谁是绑事件的。 推荐查看天气:https://www.0dtq.com/linfenyicheng/15tian.html 推荐查看天气:https://www.0dtq.com/linfenyicheng/40tian.html 推荐查看天气:https://www.0dtq.com/xiangfen/ 推荐查看天气:https://www.0dtq.com/xiangfen/zuotian.html 推荐查看天气:https://www.0dtq.com/xiangfen/7tian.html 推荐查看天气:https://www.0dtq.com/xiangfen/10tian.html 推荐查看天气:https://www.0dtq.com/xiangfen/15tian.html 推荐查看天气:https://www.0dtq.com/xiangfen/40tian.html 推荐查看天气:https://www.0dtq.com/hongdong/ 推荐查看天气:https://www.0dtq.com/hongdong/zuotian.html 推荐查看天气:https://www.0dtq.com/hongdong/7tian.html 推荐查看天气:https://www.0dtq.com/hongdong/10tian.html 推荐查看天气:https://www.0dtq.com/hongdong/15tian.html 推荐查看天气:https://www.0dtq.com/hongdong/40tian.html 推荐查看天气:https://www.0dtq.com/guxian/ 推荐查看天气:https://www.0dtq.com/guxian/zuotian.html 推荐查看天气:https://www.0dtq.com/guxian/7tian.html 推荐查看天气:https://www.0dtq.com/guxian/10tian.html 推荐查看天气:https://www.0dtq.com/guxian/15tian.html 推荐查看天气:https://www.0dtq.com/guxian/40tian.html 推荐查看天气:https://www.0dtq.com/anze/ 推荐查看天气:https://www.0dtq.com/anze/zuotian.html 推荐查看天气:https://www.0dtq.com/anze/7tian.html 推荐查看天气:https://www.0dtq.com/anze/10tian.html 推荐查看天气:https://www.0dtq.com/anze/15tian.html 推荐查看天气:https://www.0dtq.com/anze/40tian.html 推荐查看天气:https://www.0dtq.com/linfenfushan/ 推荐查看天气:https://www.0dtq.com/linfenfushan/zuotian.html 推荐查看天气:https://www.0dtq.com/linfenfushan/7tian.html 推荐查看天气:https://www.0dtq.com/linfenfushan/10tian.html 推荐查看天气:https://www.0dtq.com/linfenfushan/15tian.html 推荐查看天气:https://www.0dtq.com/linfenfushan/40tian.html 推荐查看天气:https://www.0dtq.com/linfenjixian/ 推荐查看天气:https://www.0dtq.com/linfenjixian/zuotian.html 推荐查看天气:https://www.0dtq.com/linfenjixian/7tian.html 推荐查看天气:https://www.0dtq.com/linfenjixian/10tian.html 推荐查看天气:https://www.0dtq.com/linfenjixian/15tian.html 推荐查看天气:https://www.0dtq.com/linfenjixian/40tian.html 推荐查看天气:https://www.0dtq.com/xiangning/ 推荐查看天气:https://www.0dtq.com/xiangning/zuotian.html 推荐查看天气:https://www.0dtq.com/xiangning/7tian.html 推荐查看天气:https://www.0dtq.com/xiangning/10tian.html 推荐查看天气:https://www.0dtq.com/xiangning/15tian.html 推荐查看天气:https://www.0dtq.com/xiangning/40tian.html 推荐查看天气:https://www.0dtq.com/daning/ 推荐查看天气:https://www.0dtq.com/daning/zuotian.html 推荐查看天气:https://www.0dtq.com/daning/7tian.html 推荐查看天气:https://www.0dtq.com/daning/10tian.html 推荐查看天气:https://www.0dtq.com/daning/15tian.html 推荐查看天气:https://www.0dtq.com/daning/40tian.html 推荐查看天气:https://www.0dtq.com/linfenxixian/ 推荐查看天气:https://www.0dtq.com/linfenxixian/zuotian.html 推荐查看天气:https://www.0dtq.com/linfenxixian/7tian.html 推荐查看天气:https://www.0dtq.com/linfenxixian/10tian.html 推荐查看天气:https://www.0dtq.com/linfenxixian/15tian.html 推荐查看天气:https://www.0dtq.com/linfenxixian/40tian.html 推荐查看天气:https://www.0dtq.com/yonghe/ 推荐查看天气:https://www.0dtq.com/yonghe/zuotian.html 推荐查看天气:https://www.0dtq.com/yonghe/7tian.html 推荐查看天气:https://www.0dtq.com/yonghe/10tian.html 推荐查看天气:https://www.0dtq.com/yonghe/15tian.html 推荐查看天气:https://www.0dtq.com/yonghe/40tian.html 推荐查看天气:https://www.0dtq.com/puxian/ 推荐查看天气:https://www.0dtq.com/puxian/zuotian.html 推荐查看天气:https://www.0dtq.com/puxian/7tian.html 推荐查看天气:https://www.0dtq.com/puxian/10tian.html 推荐查看天气:https://www.0dtq.com/puxian/15tian.html 推荐查看天气:https://www.0dtq.com/puxian/40tian.html 推荐查看天气:https://www.0dtq.com/fenxi/ 推荐查看天气:https://www.0dtq.com/fenxi/zuotian.html 推荐查看天气:https://www.0dtq.com/fenxi/7tian.html 推荐查看天气:https://www.0dtq.com/fenxi/10tian.html 推荐查看天气:https://www.0dtq.com/fenxi/15tian.html 推荐查看天气:https://www.0dtq.com/fenxi/40tian.html 推荐查看天气:https://www.0dtq.com/houma/ 推荐查看天气:https://www.0dtq.com/houma/zuotian.html 推荐查看天气:https://www.0dtq.com/houma/7tian.html 推荐查看天气:https://www.0dtq.com/houma/10tian.html 推荐查看天气:https://www.0dtq.com/houma/15tian.html 推荐查看天气:https://www.0dtq.com/houma/40tian.html 推荐查看天气:https://www.0dtq.com/huozhou/ 推荐查看天气:https://www.0dtq.com/huozhou/zuotian.html 推荐查看天气:https://www.0dtq.com/huozhou/7tian.html 推荐查看天气:https://www.0dtq.com/huozhou/10tian.html 推荐查看天气:https://www.0dtq.com/huozhou/15tian.html 推荐查看天气:https://www.0dtq.com/huozhou/40tian.html 推荐查看天气:https://www.0dtq.com/lvliang/ 推荐查看天气:https://www.0dtq.com/lvliang/zuotian.html 推荐查看天气:https://www.0dtq.com/lvliang/7tian.html 推荐查看天气:https://www.0dtq.com/lvliang/10tian.html 推荐查看天气:https://www.0dtq.com/lvliang/15tian.html 推荐查看天气:https://www.0dtq.com/lvliang/40tian.html 推荐查看天气:https://www.0dtq.com/lishi/ 推荐查看天气:https://www.0dtq.com/lishi/zuotian.html 推荐查看天气:https://www.0dtq.com/lishi/7tian.html 推荐查看天气:https://www.0dtq.com/lishi/10tian.html 推荐查看天气:https://www.0dtq.com/lishi/15tian.html 推荐查看天气:https://www.0dtq.com/lishi/40tian.html 推荐查看天气:https://www.0dtq.com/wenshui/ 推荐查看天气:https://www.0dtq.com/wenshui/zuotian.html 推荐查看天气:https://www.0dtq.com/wenshui/7tian.html 推荐查看天气:https://www.0dtq.com/wenshui/10tian.html 推荐查看天气:https://www.0dtq.com/wenshui/15tian.html 推荐查看天气:https://www.0dtq.com/wenshui/40tian.html 推荐查看天气:https://www.0dtq.com/lvliangjiaocheng/ 推荐查看天气:https://www.0dtq.com/lvliangjiaocheng/zuotian.html 推荐查看天气:https://www.0dtq.com/lvliangjiaocheng/7tian.html 推荐查看天气:https://www.0dtq.com/lvliangjiaocheng/10tian.html 推荐查看天气:https://www.0dtq.com/lvliangjiaocheng/15tian.html 推荐查看天气:https://www.0dtq.com/lvliangjiaocheng/40tian.html 推荐查看天气:https://www.0dtq.com/xingxian/ 推荐查看天气:https://www.0dtq.com/xingxian/zuotian.html 推荐查看天气:https://www.0dtq.com/xingxian/7tian.html 推荐查看天气:https://www.0dtq.com/xingxian/10tian.html 推荐查看天气:https://www.0dtq.com/xingxian/15tian.html 推荐查看天气:https://www.0dtq.com/xingxian/40tian.html 推荐查看天气:https://www.0dtq.com/linxian/ 推荐查看天气:https://www.0dtq.com/linxian/zuotian.html 推荐查看天气:https://www.0dtq.com/linxian/7tian.html 推荐查看天气:https://www.0dtq.com/linxian/10tian.html 推荐查看天气:https://www.0dtq.com/linxian/15tian.html 推荐查看天气:https://www.0dtq.com/linxian/40tian.html 推荐查看天气:https://www.0dtq.com/liulin/ 推荐查看天气:https://www.0dtq.com/liulin/zuotian.html 推荐查看天气:https://www.0dtq.com/liulin/7tian.html 推荐查看天气:https://www.0dtq.com/liulin/10tian.html 推荐查看天气:https://www.0dtq.com/liulin/15tian.html 推荐查看天气:https://www.0dtq.com/liulin/40tian.html 推荐查看天气:https://www.0dtq.com/shilou/ 推荐查看天气:https://www.0dtq.com/shilou/zuotian.html 推荐查看天气:https://www.0dtq.com/shilou/7tian.html 推荐查看天气:https://www.0dtq.com/shilou/10tian.html 推荐查看天气:https://www.0dtq.com/shilou/15tian.html 推荐查看天气:https://www.0dtq.com/shilou/40tian.html 推荐查看天气:https://www.0dtq.com/lanxian/ 推荐查看天气:https://www.0dtq.com/lanxian/zuotian.html 推荐查看天气:https://www.0dtq.com/lanxian/7tian.html 推荐查看天气:https://www.0dtq.com/lanxian/10tian.html 推荐查看天气:https://www.0dtq.com/lanxian/15tian.html 推荐查看天气:https://www.0dtq.com/lanxian/40tian.html 推荐查看天气:https://www.0dtq.com/fangshan/ 推荐查看天气:https://www.0dtq.com/fangshan/zuotian.html 推荐查看天气:https://www.0dtq.com/fangshan/7tian.html 推荐查看天气:https://www.0dtq.com/fangshan/10tian.html 推荐查看天气:https://www.0dtq.com/fangshan/15tian.html 推荐查看天气:https://www.0dtq.com/fangshan/40tian.html 推荐查看天气:https://www.0dtq.com/zhongyang/ 推荐查看天气:https://www.0dtq.com/zhongyang/zuotian.html 推荐查看天气:https://www.0dtq.com/zhongyang/7tian.html 推荐查看天气:https://www.0dtq.com/zhongyang/10tian.html 推荐查看天气:https://www.0dtq.com/zhongyang/15tian.html 推荐查看天气:https://www.0dtq.com/zhongyang/40tian.html 推荐查看天气:https://www.0dtq.com/jiaokou/ 推荐查看天气:https://www.0dtq.com/jiaokou/zuotian.html 推荐查看天气:https://www.0dtq.com/jiaokou/7tian.html 推荐查看天气:https://www.0dtq.com/jiaokou/10tian.html 推荐查看天气:https://www.0dtq.com/jiaokou/15tian.html 推荐查看天气:https://www.0dtq.com/jiaokou/40tian.html 推荐查看天气:https://www.0dtq.com/xiaoyi/ 推荐查看天气:https://www.0dtq.com/xiaoyi/zuotian.html 推荐查看天气:https://www.0dtq.com/xiaoyi/7tian.html 推荐查看天气:https://www.0dtq.com/xiaoyi/10tian.html 推荐查看天气:https://www.0dtq.com/xiaoyi/15tian.html 推荐查看天气:https://www.0dtq.com/xiaoyi/40tian.html 推荐查看天气:https://www.0dtq.com/fenyang/ 推荐查看天气:https://www.0dtq.com/fenyang/zuotian.html 推荐查看天气:https://www.0dtq.com/fenyang/7tian.html 推荐查看天气:https://www.0dtq.com/fenyang/10tian.html 推荐查看天气:https://www.0dtq.com/fenyang/15tian.html 推荐查看天气:https://www.0dtq.com/fenyang/40tian.html 推荐查看天气:https://www.0dtq.com/huhehaote/ 推荐查看天气:https://www.0dtq.com/huhehaote/zuotian.html 推荐查看天气:https://www.0dtq.com/huhehaote/7tian.html 推荐查看天气:https://www.0dtq.com/huhehaote/10tian.html 推荐查看天气:https://www.0dtq.com/huhehaote/15tian.html 推荐查看天气:https://www.0dtq.com/huhehaote/40tian.html 推荐查看天气:https://www.0dtq.com/yuquan/ 推荐查看天气:https://www.0dtq.com/yuquan/zuotian.html 推荐查看天气:https://www.0dtq.com/yuquan/7tian.html 推荐查看天气:https://www.0dtq.com/yuquan/10tian.html 推荐查看天气:https://www.0dtq.com/yuquan/15tian.html 推荐查看天气:https://www.0dtq.com/yuquan/40tian.html 推荐查看天气:https://www.0dtq.com/saihan/ 推荐查看天气:https://www.0dtq.com/saihan/zuotian.html 推荐查看天气:https://www.0dtq.com/saihan/7tian.html 推荐查看天气:https://www.0dtq.com/saihan/10tian.html 推荐查看天气:https://www.0dtq.com/saihan/15tian.html 推荐查看天气:https://www.0dtq.com/saihan/40tian.html 推荐查看天气:https://www.0dtq.com/tumotezuoqi/ 推荐查看天气:https://www.0dtq.com/tumotezuoqi/zuotian.html 推荐查看天气:https://www.0dtq.com/tumotezuoqi/7tian.html 推荐查看天气:https://www.0dtq.com/tumotezuoqi/10tian.html 推荐查看天气:https://www.0dtq.com/tumotezuoqi/15tian.html 推荐查看天气:https://www.0dtq.com/tumotezuoqi/40tian.html 推荐查看天气:https://www.0dtq.com/tuoketuo/ 推荐查看天气:https://www.0dtq.com/tuoketuo/zuotian.html 推荐查看天气:https://www.0dtq.com/tuoketuo/7tian.html 推荐查看天气:https://www.0dtq.com/tuoketuo/10tian.html 推荐查看天气:https://www.0dtq.com/tuoketuo/15tian.html 推荐查看天气:https://www.0dtq.com/tuoketuo/40tian.html

优秀的个人博客,低调大师

JavaScript 中 Intl.NumberFormat 的实战用法:从千分位到货币与单位格式化

最近在一个后台管理项目的财务模块里,需要在页面上展示多币种金额、百分比、文件大小和紧凑数字(比如 12.3K、1.2M),后端返回的都是原始数值,前端要按不同地区用户的习惯做本地化格式化。以前团队里习惯手写 `toFixed`、字符串截取加符号,结果遇到欧元符号位置、印度数字分组(1,00,000 这种)、阿拉伯语环境下的数字方向时,代码里全是判断分支,维护起来很痛苦。后来把这类需求统一交给 `Intl.NumberFormat`,大部分逻辑都收敛成配置项了。这篇文章整理一下我在实际项目里用到的几种配置方式,包括普通数字分组、货币格式、百分比、紧凑计数,以及如何通过 `formatToParts` 做更细粒度的渲染控制。 ## 基础用法与常见参数 `Intl.NumberFormat` 是 ECMAScript 国际化 API 的一部分,现代浏览器和 Node.js(完整 ICU 版本)都支持。最基本的用法就是传入语言标签,然后调用 `format`: ```js const nf = new Intl.NumberFormat('zh-CN'); nf.format(1234567.89); // "1,234,567.89" const nfEn = new Intl.NumberFormat('en-US'); nfEn.format(1234567.89); // "1,234,567.89" const nfDe = new Intl.NumberFormat('de-DE'); nfDe.format(1234567.89); // "1.234.567,89" ``` 德语环境里千分位和小数点正好和英语相反,如果自己拼字符串很容易漏掉这种差异。`Intl.NumberFormat` 内部根据 CLDR 数据做处理,不需要我们维护规则。 几个常用的选项: - `style`:`decimal`(默认)、`currency`、`percent`、`unit` - `currency`:当 `style` 为 `currency` 时必填,ISO 4217 代码,比如 `CNY`、`USD`、`EUR` - `currencyDisplay`:`symbol`、`code`、`name`,控制显示符号还是文字 - `useGrouping`:是否启用千分位分组 - `minimumFractionDigits` / `maximumFractionDigits`:控制小数位数 - `notation`:`standard`、`scientific`、`engineering`、`compact` ## 货币格式化里容易踩的坑 做跨境电商时遇到过一个问题:同一个商品价格,美国用户看到 `$1,299.00`,法国用户看到 `1 299,00 €`,日本用户看到 `¥1,299`。如果按字符串拼接,需要维护每个币种的小数位数和符号位置。用 `Intl` 直接写: ```js function formatCurrency(value, currency, locale) { return new Intl.NumberFormat(locale, { style: 'currency', currency, // 有些业务要求固定两位小数,避免 ¥1,299 这种没有小数位的展示 minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(value); } formatCurrency(1299, 'USD', 'en-US'); // "$1,299.00" formatCurrency(1299, 'EUR', 'fr-FR'); // "1 299,00 €" formatCurrency(1299, 'JPY', 'ja-JP'); // "¥1,299.00" ``` 注意日元本身在 CLDR 里默认没有小数位,如果业务强制两位,需要通过 `minimumFractionDigits` 覆盖。另外 `currencyDisplay` 设成 `name` 时会输出“1,299.00 美元”这样的文字,在需要无障碍朗读的场景下有用。 ## 百分比和紧凑计数 百分比场景是数据看板里最常见的,比如增长率、转化率。后端通常返回 0.1234 这样的比例,前端要显示成 12.34%。 ```js const percentFmt = new Intl.NumberFormat('zh-CN', { style: 'percent', minimumFractionDigits: 1, maximumFractionDigits: 2 }); percentFmt.format(0.1234); // "12.34%" percentFmt.format(0.5); // "50.0%" ``` `style: 'percent'` 会自动把数值乘以 100 并追加百分号,不需要自己算。 紧凑计数适合展示点赞数、阅读量、文件大小这类数字。`notation: 'compact'` 会按语言习惯输出 K、M、B 或者中文的“万”“亿”: ```js const compactZh = new Intl.NumberFormat('zh-CN', { notation: 'compact', compactDisplay: 'short' }); compactZh.format(12345); // "1.2万" compactZh.format(123456789); // "1.2亿" const compactEn = new Intl.NumberFormat('en-US', { notation: 'compact' }); compactEn.format(12345); // "12K" ``` 中文环境下 `compactDisplay: 'long'` 会输出“1.2万”这种形式,`short` 在某些实现里会输出“1.2万”也可能输出“1.2万”,具体看 ICU 版本。实际项目里如果对展示文案有严格 UI 要求,建议用 `formatToParts` 拿到数字和单位分别处理。 ## 用 formatToParts 做细粒度控制 有时候设计稿要求数字用一种字体、单位用另一种字体,或者要把单位变成可点击的 tooltip。`format` 返回的是完整字符串,拿不到分段信息,这时候用 `formatToParts`: ```js const fmt = new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY', currencyDisplay: 'symbol' }); console.log(fmt.formatToParts(1234.5)); // [ // { type: 'currency', value: '¥' }, // { type: 'integer', value: '1' }, // { type: 'group', value: ',' }, // { type: 'integer', value: '234' }, // { type: 'decimal', value: '.' }, // { type: 'fraction', value: '5' } // ] ``` 在 Vue 或 React 里可以这样渲染: ```jsx function CurrencyText({ value, currency, locale }) { const parts = new Intl.NumberFormat(locale, { style: 'currency', currency }).formatToParts(value); return ( {parts.map((part, i) => ( {part.value} ))} ); } ``` 配合 CSS 就能给 `.part-currency` 单独设颜色或边距,不用去拆字符串。 ## 单位格式化的实际场景 文件大小、距离、温度这些带单位的数值,用 `style: 'unit'` 比手动拼 `MB`、`km` 更省事: ```js const byteFmt = new Intl.NumberFormat('zh-CN', { style: 'unit', unit: 'megabyte', unitDisplay: 'short' }); byteFmt.format(2.5); // "2.5 MB" const kmFmt = new Intl.NumberFormat('en-US', { style: 'unit', unit: 'kilometer', unitDisplay: 'long' }); kmFmt.format(5); // "5 kilometers" ``` `unit` 支持的单位列表在 ECMA-402 规范里有定义,常见的 `byte`、`kilobyte`、`megabyte`、`gigabyte`、`second`、`minute`、`hour`、`celsius`、`fahrenheit`、`kilometer`、`mile` 都可用。 不过要注意,`Intl.NumberFormat` 的 `unit` 并不会自动做单位换算。2.5 MB 传入 `megabyte` 就是 2.5 MB,不会帮你从字节转过来。换算逻辑还是得自己写,只是把显示部分交给它。 ## 性能与缓存 `new Intl.NumberFormat` 的构造过程涉及加载 locale 数据,虽然现代浏览器做了缓存,但在列表里对每个单元格都 new 一次仍然不划算。常见的做法是按 `locale + options` 做 key 缓存 formatter 实例: ```js const cache = new Map(); function getFormatter(locale, options) { const key = locale + JSON.stringify(options); if (!cache.has(key)) { cache.set(key, new Intl.NumberFormat(locale, options)); } return cache.get(key); } ``` 在表格里渲染几百行时,这个缓存能省掉不少重复构造。另一个细节是 `Intl.NumberFormat` 的 options 对象顺序会影响 `JSON.stringify` 的结果,如果动态拼 options,最好保证字段顺序稳定,否则缓存命中率会下降。 ## Node.js 环境下的注意点 Node.js 从 13 开始默认带 full-icu,但有些 Docker 镜像为了减小体积会裁剪掉 ICU 数据,导致 `Intl.NumberFormat` 输出英文格式甚至报错。线上服务如果做服务端渲染,构建镜像时确认 `node -p "process.config.variables.icu_small"` 是 false,或者直接用官方 node 镜像。 ```js // 检查当前 Node 的 ICU 支持情况 console.log(Intl.NumberFormat.supportedLocalesOf(['zh-CN', 'de-DE', 'ar-EG'])); ``` 如果返回数组里缺少你需要的 locale,说明 ICU 数据不全,需要换构建方式或安装 `full-icu` 包。 `Intl.NumberFormat` 不能覆盖所有格式化需求,比如自定义的“前 N 位用大字号、后 M 位用小字号”这种设计稿,还是得靠 `formatToParts` 配合样式。但常规的数字本地化、货币、百分比、紧凑计数和单位显示,用它比手写规则靠谱得多,也省掉了大量 locale 判断代码。 推荐查看天气:http://www.362tq.com/jiancaoping/ 推荐查看天气:http://www.362tq.com/jiancaoping/zuotian.html 推荐查看天气:http://www.362tq.com/jiancaoping/7tian.html 推荐查看天气:http://www.362tq.com/jiancaoping/10tian.html 推荐查看天气:http://www.362tq.com/jiancaoping/15tian.html 推荐查看天气:http://www.362tq.com/jiancaoping/40tian.html 推荐查看天气:http://www.362tq.com/wanbailin/ 推荐查看天气:http://www.362tq.com/wanbailin/zuotian.html 推荐查看天气:http://www.362tq.com/wanbailin/7tian.html 推荐查看天气:http://www.362tq.com/wanbailin/10tian.html 推荐查看天气:http://www.362tq.com/wanbailin/15tian.html 推荐查看天气:http://www.362tq.com/wanbailin/40tian.html 推荐查看天气:http://www.362tq.com/jinyuan/ 推荐查看天气:http://www.362tq.com/jinyuan/zuotian.html 推荐查看天气:http://www.362tq.com/jinyuan/7tian.html 推荐查看天气:http://www.362tq.com/jinyuan/10tian.html 推荐查看天气:http://www.362tq.com/jinyuan/15tian.html 推荐查看天气:http://www.362tq.com/jinyuan/40tian.html 推荐查看天气:http://www.362tq.com/qingxu/ 推荐查看天气:http://www.362tq.com/qingxu/zuotian.html 推荐查看天气:http://www.362tq.com/qingxu/7tian.html 推荐查看天气:http://www.362tq.com/qingxu/10tian.html 推荐查看天气:http://www.362tq.com/qingxu/15tian.html 推荐查看天气:http://www.362tq.com/qingxu/40tian.html 推荐查看天气:http://www.362tq.com/yangqu/ 推荐查看天气:http://www.362tq.com/yangqu/zuotian.html 推荐查看天气:http://www.362tq.com/yangqu/7tian.html 推荐查看天气:http://www.362tq.com/yangqu/10tian.html 推荐查看天气:http://www.362tq.com/yangqu/15tian.html 推荐查看天气:http://www.362tq.com/yangqu/40tian.html 推荐查看天气:http://www.362tq.com/loufan/ 推荐查看天气:http://www.362tq.com/loufan/zuotian.html 推荐查看天气:http://www.362tq.com/loufan/7tian.html 推荐查看天气:http://www.362tq.com/loufan/10tian.html 推荐查看天气:http://www.362tq.com/loufan/15tian.html 推荐查看天气:http://www.362tq.com/loufan/40tian.html 推荐查看天气:http://www.362tq.com/gujiao/ 推荐查看天气:http://www.362tq.com/gujiao/zuotian.html 推荐查看天气:http://www.362tq.com/gujiao/7tian.html 推荐查看天气:http://www.362tq.com/gujiao/10tian.html 推荐查看天气:http://www.362tq.com/gujiao/15tian.html 推荐查看天气:http://www.362tq.com/gujiao/40tian.html 推荐查看天气:http://www.362tq.com/datongshi/ 推荐查看天气:http://www.362tq.com/datongshi/zuotian.html 推荐查看天气:http://www.362tq.com/datongshi/7tian.html 推荐查看天气:http://www.362tq.com/datongshi/10tian.html 推荐查看天气:http://www.362tq.com/datongshi/15tian.html 推荐查看天气:http://www.362tq.com/datongshi/40tian.html 推荐查看天气:http://www.362tq.com/nanjiao/ 推荐查看天气:http://www.362tq.com/nanjiao/zuotian.html 推荐查看天气:http://www.362tq.com/nanjiao/7tian.html 推荐查看天气:http://www.362tq.com/nanjiao/10tian.html 推荐查看天气:http://www.362tq.com/nanjiao/15tian.html 推荐查看天气:http://www.362tq.com/nanjiao/40tian.html 推荐查看天气:http://www.362tq.com/xinrong/ 推荐查看天气:http://www.362tq.com/xinrong/zuotian.html 推荐查看天气:http://www.362tq.com/xinrong/7tian.html 推荐查看天气:http://www.362tq.com/xinrong/10tian.html 推荐查看天气:http://www.362tq.com/xinrong/15tian.html 推荐查看天气:http://www.362tq.com/xinrong/40tian.html 推荐查看天气:http://www.362tq.com/yanggao/ 推荐查看天气:http://www.362tq.com/yanggao/zuotian.html 推荐查看天气:http://www.362tq.com/yanggao/7tian.html 推荐查看天气:http://www.362tq.com/yanggao/10tian.html 推荐查看天气:http://www.362tq.com/yanggao/15tian.html 推荐查看天气:http://www.362tq.com/yanggao/40tian.html 推荐查看天气:http://www.362tq.com/tianzhen/ 推荐查看天气:http://www.362tq.com/tianzhen/zuotian.html 推荐查看天气:http://www.362tq.com/tianzhen/7tian.html 推荐查看天气:http://www.362tq.com/tianzhen/10tian.html 推荐查看天气:http://www.362tq.com/tianzhen/15tian.html 推荐查看天气:http://www.362tq.com/tianzhen/40tian.html 推荐查看天气:http://www.362tq.com/datongguangling/ 推荐查看天气:http://www.362tq.com/datongguangling/zuotian.html 推荐查看天气:http://www.362tq.com/datongguangling/7tian.html 推荐查看天气:http://www.362tq.com/datongguangling/10tian.html 推荐查看天气:http://www.362tq.com/datongguangling/15tian.html 推荐查看天气:http://www.362tq.com/datongguangling/40tian.html 推荐查看天气:http://www.362tq.com/lingqiu/ 推荐查看天气:http://www.362tq.com/lingqiu/zuotian.html 推荐查看天气:http://www.362tq.com/lingqiu/7tian.html 推荐查看天气:http://www.362tq.com/lingqiu/10tian.html 推荐查看天气:http://www.362tq.com/lingqiu/15tian.html 推荐查看天气:http://www.362tq.com/lingqiu/40tian.html 推荐查看天气:http://www.362tq.com/hunyuan/ 推荐查看天气:http://www.362tq.com/hunyuan/zuotian.html 推荐查看天气:http://www.362tq.com/hunyuan/7tian.html 推荐查看天气:http://www.362tq.com/hunyuan/10tian.html 推荐查看天气:http://www.362tq.com/hunyuan/15tian.html 推荐查看天气:http://www.362tq.com/hunyuan/40tian.html 推荐查看天气:http://www.362tq.com/zuoyun/ 推荐查看天气:http://www.362tq.com/zuoyun/zuotian.html 推荐查看天气:http://www.362tq.com/zuoyun/7tian.html 推荐查看天气:http://www.362tq.com/zuoyun/10tian.html 推荐查看天气:http://www.362tq.com/zuoyun/15tian.html 推荐查看天气:http://www.362tq.com/zuoyun/40tian.html 推荐查看天气:http://www.362tq.com/datongxian/ 推荐查看天气:http://www.362tq.com/datongxian/zuotian.html 推荐查看天气:http://www.362tq.com/datongxian/7tian.html 推荐查看天气:http://www.362tq.com/datongxian/10tian.html 推荐查看天气:http://www.362tq.com/datongxian/15tian.html 推荐查看天气:http://www.362tq.com/datongxian/40tian.html 推荐查看天气:http://www.362tq.com/yangquan/ 推荐查看天气:http://www.362tq.com/yangquan/zuotian.html 推荐查看天气:http://www.362tq.com/yangquan/7tian.html 推荐查看天气:http://www.362tq.com/yangquan/10tian.html 推荐查看天气:http://www.362tq.com/yangquan/15tian.html 推荐查看天气:http://www.362tq.com/yangquan/40tian.html 推荐查看天气:http://www.362tq.com/kuangqu/ 推荐查看天气:http://www.362tq.com/kuangqu/zuotian.html 推荐查看天气:http://www.362tq.com/kuangqu/7tian.html 推荐查看天气:http://www.362tq.com/kuangqu/10tian.html 推荐查看天气:http://www.362tq.com/kuangqu/15tian.html 推荐查看天气:http://www.362tq.com/kuangqu/40tian.html 推荐查看天气:http://www.362tq.com/pingding/ 推荐查看天气:http://www.362tq.com/pingding/zuotian.html 推荐查看天气:http://www.362tq.com/pingding/7tian.html 推荐查看天气:http://www.362tq.com/pingding/10tian.html 推荐查看天气:http://www.362tq.com/pingding/15tian.html 推荐查看天气:http://www.362tq.com/pingding/40tian.html 推荐查看天气:http://www.362tq.com/yuxian/ 推荐查看天气:http://www.362tq.com/yuxian/zuotian.html 推荐查看天气:http://www.362tq.com/yuxian/7tian.html 推荐查看天气:http://www.362tq.com/yuxian/10tian.html 推荐查看天气:http://www.362tq.com/yuxian/15tian.html 推荐查看天气:http://www.362tq.com/yuxian/40tian.html 推荐查看天气:http://www.362tq.com/changzhi/ 推荐查看天气:http://www.362tq.com/changzhi/zuotian.html 推荐查看天气:http://www.362tq.com/changzhi/7tian.html 推荐查看天气:http://www.362tq.com/changzhi/10tian.html 推荐查看天气:http://www.362tq.com/changzhi/15tian.html 推荐查看天气:http://www.362tq.com/changzhi/40tian.html 推荐查看天气:http://www.362tq.com/xiangyuan/ 推荐查看天气:http://www.362tq.com/xiangyuan/zuotian.html 推荐查看天气:http://www.362tq.com/xiangyuan/7tian.html 推荐查看天气:http://www.362tq.com/xiangyuan/10tian.html 推荐查看天气:http://www.362tq.com/xiangyuan/15tian.html 推荐查看天气:http://www.362tq.com/xiangyuan/40tian.html 推荐查看天气:http://www.362tq.com/tunliu/ 推荐查看天气:http://www.362tq.com/tunliu/zuotian.html 推荐查看天气:http://www.362tq.com/tunliu/7tian.html 推荐查看天气:http://www.362tq.com/tunliu/10tian.html 推荐查看天气:http://www.362tq.com/tunliu/15tian.html 推荐查看天气:http://www.362tq.com/tunliu/40tian.html 推荐查看天气:http://www.362tq.com/pingshun/ 推荐查看天气:http://www.362tq.com/pingshun/zuotian.html 推荐查看天气:http://www.362tq.com/pingshun/7tian.html 推荐查看天气:http://www.362tq.com/pingshun/10tian.html 推荐查看天气:http://www.362tq.com/pingshun/15tian.html 推荐查看天气:http://www.362tq.com/pingshun/40tian.html 推荐查看天气:http://www.362tq.com/changzhilicheng/ 推荐查看天气:http://www.362tq.com/changzhilicheng/zuotian.html 推荐查看天气:http://www.362tq.com/changzhilicheng/7tian.html 推荐查看天气:http://www.362tq.com/changzhilicheng/10tian.html 推荐查看天气:http://www.362tq.com/changzhilicheng/15tian.html 推荐查看天气:http://www.362tq.com/changzhilicheng/40tian.html 推荐查看天气:http://www.362tq.com/huguan/ 推荐查看天气:http://www.362tq.com/huguan/zuotian.html 推荐查看天气:http://www.362tq.com/huguan/7tian.html 推荐查看天气:http://www.362tq.com/huguan/10tian.html 推荐查看天气:http://www.362tq.com/huguan/15tian.html 推荐查看天气:http://www.362tq.com/huguan/40tian.html 推荐查看天气:http://www.362tq.com/changzi/ 推荐查看天气:http://www.362tq.com/changzi/zuotian.html 推荐查看天气:http://www.362tq.com/changzi/7tian.html 推荐查看天气:http://www.362tq.com/changzi/10tian.html 推荐查看天气:http://www.362tq.com/changzi/15tian.html 推荐查看天气:http://www.362tq.com/changzi/40tian.html 推荐查看天气:http://www.362tq.com/wuxiang/ 推荐查看天气:http://www.362tq.com/wuxiang/zuotian.html 推荐查看天气:http://www.362tq.com/wuxiang/7tian.html 推荐查看天气:http://www.362tq.com/wuxiang/10tian.html 推荐查看天气:http://www.362tq.com/wuxiang/15tian.html 推荐查看天气:http://www.362tq.com/wuxiang/40tian.html 推荐查看天气:http://www.362tq.com/qinxian/ 推荐查看天气:http://www.362tq.com/qinxian/zuotian.html 推荐查看天气:http://www.362tq.com/qinxian/7tian.html 推荐查看天气:http://www.362tq.com/qinxian/10tian.html 推荐查看天气:http://www.362tq.com/qinxian/15tian.html 推荐查看天气:http://www.362tq.com/qinxian/40tian.html 推荐查看天气:http://www.362tq.com/qinyuan/ 推荐查看天气:http://www.362tq.com/qinyuan/zuotian.html 推荐查看天气:http://www.362tq.com/qinyuan/7tian.html 推荐查看天气:http://www.362tq.com/qinyuan/10tian.html 推荐查看天气:http://www.362tq.com/qinyuan/15tian.html 推荐查看天气:http://www.362tq.com/qinyuan/40tian.html 推荐查看天气:http://www.362tq.com/shuozhou/ 推荐查看天气:http://www.362tq.com/shuozhou/zuotian.html 推荐查看天气:http://www.362tq.com/shuozhou/7tian.html 推荐查看天气:http://www.362tq.com/shuozhou/10tian.html 推荐查看天气:http://www.362tq.com/shuozhou/15tian.html 推荐查看天气:http://www.362tq.com/shuozhou/40tian.html 推荐查看天气:http://www.362tq.com/shuocheng/ 推荐查看天气:http://www.362tq.com/shuocheng/zuotian.html 推荐查看天气:http://www.362tq.com/shuocheng/7tian.html 推荐查看天气:http://www.362tq.com/shuocheng/10tian.html 推荐查看天气:http://www.362tq.com/shuocheng/15tian.html 推荐查看天气:http://www.362tq.com/shuocheng/40tian.html 推荐查看天气:http://www.362tq.com/shuozhoupinglu/ 推荐查看天气:http://www.362tq.com/shuozhoupinglu/zuotian.html

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册