JavaScript 数字格式化、URI 编码和 CSS 选择器
显示数字可用 toLocaleString(),编码单个 URI 组件可用 encodeURIComponent(),构建查询参数可用 URL.searchParams。CSS 和 querySelectorAll() 使用相同的选择器语法。
格式化要显示的数字
区域设置决定数字分组和小数分隔符。en-US 将 123456789 显示为 123,456,789,de-DE 将 1234.5 显示为 1.234,5。返回的是字符串,计算时应保留原始数值。 ECMA-402 Number.prototype.toLocaleString
console.log((123456789).toLocaleString("en-US"));
console.log((1234.5).toLocaleString("de-DE"));
区分完整 URI 和单个组件
URI 用于标识资源。encodeURI() 保留 ?、&、# 等结构字符,encodeURIComponent() 则会编码一个值中的这些字符。值中未经编码的 & 可能被当成参数分隔符。decodeURI() 保留预留分隔符,decodeURIComponent() 解码单个组件。 ECMAScript URI Handling Functions
console.log(encodeURI("https://example.com/search?q=coffee&sort=new"));
console.log(encodeURIComponent("coffee&tea"));
console.log(decodeURIComponent("coffee%26tea"));
用原始值构建查询参数
给 searchParams.set() 传入原始文本。URLSearchParams 序列化时将空格写成 +,实际的加号写成 %2B。可用 get() 读取原值。如果传入已经编码的 coffee%26tea,百分号会再次编码,结果为 coffee%2526tea。 WHATWG URLSearchParams
const url = new URL("https://example.com/search");
url.searchParams.set("q", "coffee & tea");
url.searchParams.set("page", "1");
console.log(url.href);
console.log(url.searchParams.get("q"));
处理解码错误和旧数据
格式错误的百分号序列或无效的 UTF-8 可能触发 URIError。示例只在此时返回 null,其他错误会继续抛出。应按参数解析查询字符串,再读取各项。旧的 escape() 可能生成 %uXXXX 格式,请在迁移前确认来源。 ECMAScript URI Handling Functions
function decodeComponentSafely(text) {
try {
return decodeURIComponent(text);
} catch (error) {
if (error instanceof URIError) return null;
throw error;
}
}
console.log(decodeComponentSafely("%E0%A4%A"));
按位置选择子元素
li:nth-child(2) 要求第二个子元素是 li。li:nth-of-type(2) 只数同类型的 li 兄弟。文本和注释不计入。可在 div 等允许混合元素的容器内对比两种行为。selectivizr 是面向旧版 Internet Explorer 的方案。 W3C Selectors Level 4
#items > li:nth-child(2) {
color: #a00000;
}
#items > li:last-child {
font-weight: bold;
}
运行完整 HTML 示例
将代码以 UTF-8 保存为 javascript-notes.html,并在当前浏览器中打开。页面只显示 URL,不发起搜索。输入 coffee & tea 时,组件包含 %20 和 %26,查询字符串以 + 表示空格。解码行恢复原值,CSS 会突出显示第二项。
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JavaScript 数字格式化、URI 编码和 CSS 选择器</title>
<style>
.selected { color: #a00000; font-weight: bold; }
#output { white-space: pre-wrap; overflow-wrap: anywhere; }
</style>
</head>
<body>
<label for="query">搜索词</label>
<input id="query" value="coffee & tea">
<button id="build" type="button">生成 URL</button>
<ul id="items"><li>第一项</li><li>第二项</li><li>第三项</li></ul>
<pre id="output" aria-live="polite"></pre>
<script>
const input = document.getElementById("query");
const output = document.getElementById("output");
function show() {
const value = input.value;
const component = encodeURIComponent(value);
const url = new URL("https://example.com/search");
url.searchParams.set("q", value);
output.textContent = [
"组件编码" + ": " + component,
"解码结果" + ": " + decodeURIComponent(component),
"搜索 URL" + ": " + url.href,
"格式化后的数字" + ": " + (123456789).toLocaleString("en-US")
].join("\n");
}
for (const item of document.querySelectorAll("#items > li:nth-child(2)")) {
item.classList.add("selected");
}
document.getElementById("build").addEventListener("click", show);
show();
</script>
</body>
</html>




