DrillLab
八股题库Question bank

105 道问答题,一道一卡105 questions, one card each

默认只显示问题 —— 先自己在心里答一遍,再展开对答案。答不上来就标「不会」,下次抽认卡会先抽它。Only the question shows by default. Answer it in your head first, then open the answer. Mark the ones you miss; the flashcard round puts those first.

0 / 105道自评过self-assessed
0Got it0模糊Shaky0不会No idea105还没做Not seen
正在读这台浏览器里的标记…Reading your marks from this browser…
标记不是打分,是给下一轮排队Marks are a queue, not a score

标「不会」的题会排到抽认卡最前面,标「会」的进低频池排最后。所以别客气 —— 觉得答得磕磕巴巴就标「模糊」。准备好了就去抽认卡“No idea” jumps to the front of the next round; “got it” drops to the back. So be honest — if it came out shaky, mark it shaky. Then go run a flashcard round.

题目Questions

筛出 5 道(共 105 道)。5 of 105 questions.
HTMLHTML#269

块级元素 vs 行内元素

Block element vs Inline element

看答案Show answer

一句话:块级元素独占一行、宽高可控; 行内元素跟着文字流走、宽高由内容决定。

具体差别就三处,记住这三条就够答:

  • 换行—— 块级前后自动断行(divph1ul); 行内不断行(spanastrongimg)。
  • 宽高—— 块级可以设 width /height;行内设了不生效
  • 内外边距—— 块级四个方向都生效; 行内的上下 margin 不生效, 上下 padding 视觉上会溢出但不撑开行高。

会追问:「那 inline-block 呢?」 —— 它是折中:不换行(像行内),但宽高和上下 margin 都生效(像块级)。 导航按钮常用它。

还会追问:img 是行内元素, 为什么能设宽高?」—— 因为它是替换元素(replaced element), 内容由外部资源决定,浏览器对它网开一面。inputvideo 同理。 这个点答出来会加分。

In one line: a block element takes the whole line and you can set its width and height; an inline element flows with the text and is sized by its content.

There are exactly three differences worth remembering:

  • Line breaks — block elements break before and after (div, p, h1, ul); inline ones do not (span, a, strong, img).
  • Width and height — settable on block elements, ignored on inline ones.
  • Margin and padding — all four sides work on block elements; on inline elements vertical margin does nothing, and vertical padding overflows visually without pushing the line apart.

Follow-up: “What about inline-block?” — it is the compromise: no line break (like inline) but width, height and vertical margin all work (like block). Nav buttons use it constantly.

Also asked:img is inline, so why can you set its size?” — because it is a replaced element: its content comes from an external resource, so the browser makes an exception. Same for input and video. Getting this one right scores points.

HTMLHTML#380

事件冒泡 vs 事件捕获

Event bubbling vs Event capturing

看答案Show answer

一句话:一次点击在 DOM 里走三段 —— 先从 window 往下到目标(捕获), 在目标上触发(目标阶段),再从目标往上回到window(冒泡)。捕获从外到内,冒泡从内到外。

addEventListener 第三个参数决定你在哪一段听: 默认 false 听冒泡,true(或 { capture: true })听捕获。

为什么默认是冒泡?因为绝大多数时候你想知道的是 「用户点了什么」,从内往外传最自然 —— 而且这才让事件委托成为可能(见 #289)。

会追问:「怎么阻止?」

  • e.stopPropagation()—— 别再往上(或往下)传了。
  • e.stopImmediatePropagation()—— 连同一个元素上的其他监听器都别跑了。
  • e.preventDefault()—— 阻止的是默认行为(链接跳转、表单提交), 和传播完全无关。这两个最容易混,别答错。

还会追问:「有哪些事件不冒泡?」——focusblurloadmouseentermouseleave。 需要委托时用它们的冒泡版:focusin /focusout / mouseover /mouseout

In one line: a click travels the DOM in three phases — down from window to the target (capture), fires on the target, then back up to window (bubble). Capture goes outside-in, bubbling goes inside-out.

The third argument to addEventListener picks the phase you listen in: false (the default) is bubbling, true (or { capture: true }) is capture.

Why is bubbling the default? Because what you almost always want to know is “what did the user click”, and inside-out is the natural direction for that — it is also what makes event delegation possible (see #289).

Follow-up: “How do you stop it?”

  • e.stopPropagation() — stop travelling further up (or down).
  • e.stopImmediatePropagation() — also skip other listeners on the same element.
  • e.preventDefault() — cancels the default behaviour (link navigation, form submit) and has nothing to do with propagation. These two get mixed up most often; do not confuse them.

Also asked: “Which events do not bubble?” —focus, blur, load, mouseenter, mouseleave. When you need to delegate, use their bubbling counterparts: focusin / focusout / mouseover / mouseout.

JavaScript三个阶段的完整顺序The full order of the three phases示意Illustrative
1// <div id="outer"><div id="inner"><button id="btn">点我</button></div></div>
2
3outer.addEventListener("click", () => console.log("outer 捕获"), true);
4inner.addEventListener("click", () => console.log("inner 捕获"), true);
5btn .addEventListener("click", () => console.log("btn 目标"));
6inner.addEventListener("click", () => console.log("inner 冒泡"));
7outer.addEventListener("click", () => console.log("outer 冒泡"));
8
9// 点击 btn 的输出顺序:
10// outer 捕获 -> inner 捕获 -> btn 目标 -> inner 冒泡 -> outer 冒泡
1// <div id="outer"><div id="inner"><button id="btn">Click me</button></div></div>
2
3outer.addEventListener("click", () => console.log("outer capture"), true);
4inner.addEventListener("click", () => console.log("inner capture"), true);
5btn .addEventListener("click", () => console.log("btn target"));
6inner.addEventListener("click", () => console.log("inner bubble"));
7outer.addEventListener("click", () => console.log("outer bubble"));
8
9// Output order when btn is clicked:
10// outer capture -> inner capture -> btn target -> inner bubble -> outer bubble
面试里画得出这个顺序,基本就过了。注意目标元素上的监听器不分捕获/冒泡,按注册顺序执行。If you can draw this order in an interview, that question is handled. One detail: on the target element itself there is no capture or bubble distinction, so those listeners run in the order they were registered.
HTMLHTML#381

meta 标签有什么用

What is the importance of the meta tag?

看答案Show answer

一句话:meta 放的是 「关于这个页面的信息」—— 浏览器、搜索引擎、社交平台读它,用户看不到它。

真正天天用到的就四个,答这四个就够:

  • <meta charset="UTF-8">——必须放在 head 最前面。 没有它中文会乱码,因为浏览器得先知道用什么编码去解析后面的字节。
  • viewport——移动端适配的前提。 不写这一行,手机浏览器会假装自己有 980px 宽然后整页缩小, 你写的所有响应式 CSS 全白费(见 #274)。
  • description—— 搜索结果里那段摘要。 它不影响排名,但影响点击率。
  • Open Graphog:titleog:image)—— 链接分享到微信 / Slack / Twitter 时显示的卡片。

会追问:keywords 还有用吗?」 —— 没用了,Google 早就不看,因为被滥用成了堆关键词。 这个点能答出来说明你不是背的旧教程。

In one line: meta carries information about the page — browsers, search engines and social platforms read it; users never see it.

Only four of them actually matter day to day:

  • <meta charset="UTF-8"> must be first in the head. Without it non-ASCII text turns to mojibake, because the browser has to know the encoding before it can parse the bytes that follow.
  • viewport the precondition for mobile. Without this line a phone browser pretends it is 980px wide and scales the whole page down, which throws away every responsive rule you wrote (see #274).
  • description — the snippet in search results. It does not affect ranking, but it affects click-through.
  • Open Graph (og:title, og:image) — the card shown when the link is shared into Slack, Twitter or a chat app.

Follow-up: “Does keywords still do anything?” — no. Google stopped reading it long ago because it was abused into keyword stuffing. Knowing this shows you are not reciting an old tutorial.

Text实际会写的那几行示意Illustrative
1<head>
2 <meta charset="UTF-8"> <!-- 必须最先 -->
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <meta name="description" content="一句话说明这个页面是干什么的">
5 <meta property="og:title" content="分享出去显示的标题">
6 <meta property="og:image" content="https://…/cover.png">
7 <title>页面标题</title>
8</head>
1<head>
2 <meta charset="UTF-8"> <!-- must come first -->
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <meta name="description" content="one sentence on what this page is for">
5 <meta property="og:title" content="the title shown when shared">
6 <meta property="og:image" content="https://…/cover.png">
7 <title>Page title</title>
8</head>
HTMLHTML#382

什么是语义化标签

What are Semantic Elements?

看答案Show answer

一句话:标签名本身说明了内容的角色 ——header / nav / main /article / section /aside / footer, 而 divspan 什么都没说。

关键是别答成「代码更好看」。语义化有两个可以量化的真实收益:

  • 屏幕阅读器能导航。视障用户可以按「跳到主内容」「列出所有标题」来浏览 —— 这些功能依赖标签语义。 一整页 div,读屏软件只能从头念到尾。
  • 搜索引擎知道哪块是正文。main 里的内容权重高于 aside

会追问:sectiondiv 到底怎么选?」—— 判断标准是「这块内容有没有自己的标题」。 有(能配一个 h2)就用 section; 纯粹为了布局套一层就用 div为了样式而套的容器就该是 div, 硬换成 section 反而污染了大纲。

In one line: the tag name itself states the role of the content — header / nav / main /article / section / aside /footer — whereas div and span say nothing at all.

Do not answer “the code looks nicer”. Semantics buys you two things you can actually measure:

  • Screen readers can navigate. A blind user can jump to the main content or list every heading — and those features depend on tag semantics. On a page of nothing but divs, a screen reader can only read from the top.
  • Search engines know which part is the article. Content in main weighs more than content in aside.

Follow-up: “So how do you choose between section and div?” — the test is “does this block have its own heading?” If it does (you could give it an h2), use section. If it is a wrapper you added purely for layout, use div. A container that exists for styling should be a div — forcing it to be a section just pollutes the document outline.

HTMLHTML#385

无障碍、可用性、包容性

Could you explain accessibility, usability, and inclusion? Give some examples of each one in terms of web design.

看答案Show answer

一句话区分:

  • 无障碍(accessibility, a11y)——有障碍的人能不能用。视障、听障、 运动障碍、认知障碍。
  • 可用性(usability)——能用的人用得顺不顺。找得到、看得懂、不容易点错。
  • 包容性(inclusion)——范围够不够宽。老年人、网速慢的、 用小屏手机的、非母语用户、临时单手抱着孩子操作的。

三者是包含关系:无障碍是包容性的一部分, 可用性差的东西对所有人都差。

每个给一个例子(面试要的就是例子):

  • a11y:给 imgalt;表单 labelhtmlFor 关联到 input保证键盘能 Tab 到所有交互元素, 并且焦点环别用 outline: none 删掉。
  • usability:按钮文字写「保存草稿」 而不是「提交」;表单报错指向出错的那个字段, 而不是页面顶部一句「表单有误」。
  • inclusion:正文对比度至少 4.5:1; 不用颜色作为唯一的信息载体 (红绿色盲看不出「红色是错的」,要同时给图标或文字); 首屏在 3G 下也能出内容。

会追问:「怎么测?」—— 键盘走一遍(不碰鼠标能不能完成主流程)、 Chrome DevTools 的 Lighthouse 跑一次 a11y 评分、axe 插件扫一遍。说得出工具名比说概念更可信。

The distinction in one line each:

  • Accessibility (a11y) can people with disabilities use it: vision, hearing, motor, cognitive.
  • Usability how smoothly can people who can use it, use it: can they find things, understand them, avoid mis-taps.
  • Inclusion how wide is the net: older users, slow connections, small screens, non-native speakers, someone operating one-handed while holding a child.

The three nest: accessibility is part of inclusion, and anything with poor usability is worse for everyone.

One example each — examples are what the interviewer wants:

  • a11y: write alt on images; tie a label to its input with htmlFor; make sure Tab reaches every interactive element and do not delete the focus ring with outline: none.
  • usability: label the button “Save draft” rather than “Submit”; put the validation error on the field that failed, not a generic banner at the top of the page.
  • inclusion: at least 4.5:1 contrast for body text; never use colour as the only carrier of meaning (someone with red-green colour blindness cannot see that “red means wrong”, so add an icon or a word); make the first screen usable on 3G.

Follow-up: “How do you test it?” — walk the main flow with the keyboard only; run Lighthouse’s a11y audit in Chrome DevTools; scan with the axe extension. Naming the tools is more convincing than naming the concepts.

这些题从哪来Where these come from

99 道来自面试题库 #269–#387;TypeScript 深度那 6 道是 DrillLab 自出的(senior 补强,卡片上有标注)。答案都是 DrillLab 写的,所以讲解里的代码块一律标「示意」。每道题都能点回它出处的那一节课。99 questions come from the interview bank (#269–#387); the 6 TypeScript deep-dive ones are DrillLab-made (marked on the card). All answers are written by DrillLab, so every code block here is labelled “demo”. Each card links back to the lesson it came from.