Skip to content

浏览器 DevTools

学习到 use 浏览器 DevTools — 构建-in toolkit for inspecting, 调试, and profiling web pages.

你将学到

  • 什么 DevTools 是 and 如何 打开它们
  • 元素 panel for inspecting HTML and CSS
  • Console for 运行 JavaScript and 读取错误
  • Network panel for watching HTTP 请求

概念讲解

Browser DevTools

DevTools are the built-in developer tools in every modern browser. Press F12 or Ctrl+Shift+I (Cmd+Option+I on Mac) and a panel opens with everything you need to inspect, debug, and optimize a web page. They are the most important tool in a web developer's daily workflow — and they are free and always available.

Opening DevTools

  • F12 or Ctrl+Shift+I (Windows/Linux)
  • Cmd+Option+I (Mac)
  • Right-click an element → "Inspect"

The Core Panels

Elements — inspect and edit the live DOM and CSS. Hover over elements to highlight them on the page. Edit styles in real time and see the result instantly — perfect for tweaking layout. Changes do not persist (they reset on reload), so copy them to your source files.

Console — run JavaScript in the page's context and read messages. console.log output, errors, and warnings all appear here. You can also type expressions to inspect variables or test functions. The Console is your REPL for the page.

Network — watch every HTTP request the page makes. See the URL, method, status code, size, and timing. Click a request to inspect its headers and response body. This is how you debug API calls and slow loading.

Sources — view the page's JavaScript source, set breakpoints, and step through execution. Breakpoints pause the code so you can inspect variables at that moment — far more powerful than console.log.

Application — inspect storage: cookies, localStorage, IndexedDB, and the cache. Useful for debugging authentication and offline behavior.

A Daily Workflow

  1. Something looks wrong → open Elements, find the element, tweak CSS live.
  2. A button does not work → open Console, look for errors, test the handler.
  3. Data does not load → open Network, find the failed request, read the status and response.
  4. Logic is wrong → open Sources, set a breakpoint, step through and inspect variables.

DevTools turn guessing into seeing. The faster you are with them, the faster you debug.

示例代码

              // Code you might debug in DevTools.
// Set a breakpoint on the line inside the handler to inspect 'items'.

function renderCart(items) {
  const total = items.reduce((sum, i) => sum + i.price * i.qty, 0);
  console.log("Cart total:", total);  // visible in the Console panel
  document.getElementById("cart-total").textContent = `$ ${total.toFixed(2)}`;
}

// In the Console, you can run this directly to test:
// renderCart([{ price: 10, qty: 2 }, { price: 5, qty: 1 }])
            

console.log 输出 appears in Console panel. 设置 breakpoint on reduce 行 in 源 panel 到暂停 execution and inspect 项数组 — 否 logging 需要.

动手试一试

  • Markdown Previewer

    打开这个 tool, 然后打开 DevTools → Network. 类型 in box and watch: 否 network 请求 — 它所有运行客户端-边.

  • JSON Formatter

    打开 DevTools → 元素 on 这个 page. Inspect 格式化 JSON 输出到 see 如何 DOM 是 structured.

常见错误

错误写法

Relying 仅 on console.log 相反的 学习 breakpoints.

正确做法

Breakpoints 暂停 execution and let 你 inspect 所有变量 at 那个点, 步骤行 通过行, and watch 表达式. 它们是 远更多 efficient than sprinkling logs everywhere.

错误写法

Forgetting 那个 DevTools CSS 更改做 不 persist.

正确做法

DevTools 编辑是 live 但 temporary — 它们 vanish on reload. 一次你 查找正确值, 复制它们到 你的 .CSS file. Use DevTools as scratchpad, 不源 的 truth.

相关速查表

练习 浏览器 DevTools

2 个练习

练习 浏览器 DevTools

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误