来源于:https://developers.google.com/web/tools/chrome-devtools/inspect-styles/edit-dom

The DOM tree view in the Chrome DevTools Elements panel displays the DOM structure of the current web page. Live-edit the content and structure of your page through DOM updates.

TL;DR

  • The DOM defines your page structure. Each DOM node is a page element, for example, a header node, paragraph node.
  • Live-edit the content and structure of your pages through the rendered DOM.
  • But remember, you can't modify source files through DOM changes in the Elements panel. Reloading the page erases any DOM tree modifications.
  • Watch for changes to the DOM using DOM breakpoints.

Inspect an element

Use the Elements panel to inspect all elements in your page in one DOM tree. Select any element and inspect the styles applied to it.

There are several ways to inspect an element:

Right-click any element on the page and select Inspect.

Press Ctrl + Shift + C (Windows) or Cmd + Shift + C (Mac) to open DevTools in Inspect Element mode, then hover over an element. DevTools automatically highlights the element that you are hovering over in the Elements panel. Click on the element to exit inspect mode while keeping the element highlighted within the Elements panel.

Click the Inspect Element button  to go into Inspect Element Mode, then click on an element.

Use the inspect method in the console, such as inspect(document.body).

Navigate the DOM

Navigate through the DOM structure using your mouse or keyboard.

A collapsed node has an arrow next to it pointing right: 

An expanded node has an arrow next to it pointing down: 

Using your mouse:

  • Click once to highlight a node.
  • To expand a node, double-click anywhere on it or click on the arrow next
    to it.
  • To collapse a node, click on the arrow next to it.

Using your keyboard:

  • Press the Up Arrow key to select the node above the current one.
  • Press the Down Arrow to select the node below the current one.
  • Press the Right Arrow key to expand a collapsed node. Press it again to move to the first child of the (now-expanded) node. You can use this technique to quickly navigate deeply-nested nodes.

Navigate the breadcrumb trail

At the bottom of the Elements panel is a breadcrumb trail.

The currently selected node is highlighted in blue. The node to the left is the current node's parent. And to the left of that is the parent's parent. And so on, all the way up the tree.

Navigating back up the structure moves the highlight:

DevTools displays as many items as possible in the trail. If the entire trail doesn't fit in the status bar, an ellipsis (...) shows where the trail has been truncated. Click the ellipsis to show the hidden elements:

Edit DOM nodes and attributes

To edit a DOM node name or attribute:

  • Double-click directly on the node name or attribute.
  • Highlight the node, press Enter, and then press Tab until the name or attribute is selected.
  • Open the more actions menu and select Add Attribute or Edit AttributeEdit Attribute is context-sensitive; the portion you click on determines what gets edited.

The closing tag is automatically updated when you're finished.

Edit DOM node and its children as HTML

To edit a DOM node and its children as HTML:

  • Open the more actions menu and select Edit as HTML.
  • Press F2 (Windows / Linux) or Fn+F2 (Mac).
  • Press Ctrl+Enter (Windows / Linux) or Cmd+Enter (Mac) to save your changes.
  • Press Esc to exit the editor without saving.

Move DOM node

Click, hold, and drag a node to move it.

Delete DOM node

To delete a DOM node:

  • Open the more actions menu and select Delete Node.
  • Select the node and press the Delete key.

Note: If you delete a node by accident, Ctrl + Z (or Cmd + Z on Mac) to undo your last action.

Show more actions menu

The more actions menu lets you interact with a DOM node in a variety of ways. To view the menu, right-click on a node, or select a node and then press the more actions button ()). The button is only displayed on the currently selected element.

Scroll into view

When you hover over or select a DOM node, the rendered node is highlighted in the viewport. If the node is scrolled offscreen, you'll see a tooltip at the top of the viewport if the node is above the current viewport, and a tooltip at the bottom if the node is below the current viewport. For example, in the screenshot below DevTools is indicating that the currently selected element in the Elements panel is below the viewport.

To scroll the page so the node appears in the viewport, Right-click the node and select Scroll into View.

Set DOM breakpoints

Set DOM breakpoints to debug complex JavaScript applications. For example, if your JavaScript is changing the styling of a DOM element, set a DOM breakpoint to fire when the element's attributes are modified. Trigger a breakpoint on one of the following DOM changes: subtree change, attribute change, node removal.

Subtree Modifications

A subtree modification breakpoint is triggered when a child element is added, removed, or moved. For example, if you set a subtree modification breakpoint on the main-content element, the following code triggers the breakpoint:

 
var element = document.getElementById('main-content');
//modify the element's subtree.
var mySpan = document.createElement('span');
element.appendChild( mySpan );

Attribute Modifications

An attribute modification occurs when the attribute of an element (class, id, name) is changed dynamically:

 
var element = document.getElementById('main-content');
// class attribute of element has been modified.
element.className = 'active';

Node Removal

A node removal modification is triggered when the node in question is removed from the DOM:

 
document.getElementById('main-content').remove();

Interact with DOM breakpoints

The Elements and Sources panels both include a pane for managing your DOM breakpoints.

Each breakpoint is listed with an element identifier and the breakpoint type.

Interact with each listed breakpoint in any of the following ways:

  • Hover over the element identifier to show the element's corresponding position on the page (similar to hovering over nodes in the Elements panel).
  • Click an element to select it in the Elements panel.
  • Toggle the checkbox to enable or disable the breakpoint.

When you trigger a DOM breakpoint, the breakpoint is highlighted in the DOM Breakpoints pane. The Call Stack pane displays the reason for a debugger pause:

View element event listeners

View JavaScript event listeners associated with a DOM node in the Event Listeners pane.

The top-level items in the Event Listeners pane show the event types that have registered listeners.

Click the arrow next to the event type (for example click) to see a list of registered event handlers. Each handler is identified by a CSS selector-like element identifier, such as document orbutton#call-to-action. If more than one handler is registered for the same element, the element is listed repeatedly.

Click the expander arrow next to an element identifier to see the properties of the event handler. The Event Listeners pane lists the following properties for each listener:

Event Listener Properties & Description
handler Contains a callback function. Right-click on the function and select Show Function Definition to view where the function is defined (if source code is available).
useCapture A boolean value stating whether the useCapture flag on addEventListenerwas set.

Note: Many Chrome extensions add their own event listeners onto the DOM. If you see a number of event listeners that aren't set by your code, you may want to reopen your page in an Incognito window. Incognito windows prevent extensions from running by default.

View ancestor event listeners

When the Ancestors checkbox is enabled, the event listeners for the ancestors of the currently selected node are displayed, in addition to the currently selected node's event listeners.

When the checkbox is disabled, only the event listeners for the currently selected node are displayed.

View framework listeners

Some JavaScript frameworks and libraries wrap native DOM events into their custom event APIs. In the past this made it hard to inspect the event listeners with DevTools, because the function definition would just reference back to the framework or library code. The Framework listeners feature solves this problem.

When the Framework listeners checkbox is enabled, DevTools automatically resolves the framework or library wrapping portion of the event code, and then tells you where you actually bound the event in your own code.

When the Framework listeners checkbox is disabled, the event listener code will probably resolve somewhere in the framework or library code.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

chrome编辑DOM的更多相关文章

  1. Chrome开发工具Elements面板(编辑DOM和CSS样式)详解

    Element 译为“元素”,Element 面板可以让我们动态查看和编辑DOM节点和CSS样式表,并且立即生效,避免了频繁切换浏览器和编辑器的麻烦. 我们可以使用Element面板来查看源代码,它不 ...

  2. Chrome编辑了样式或者JS之后自动同步回本地工程

    比如我用HBuilder进行调试,在Chrome上修改了几个样式,我们通常的做法是,拷贝修改后的地方,然后再进行覆盖. 现在有了全自动的操作,在Chrome上修改了样式之后,通过Sources的Sav ...

  3. 从Chrome源码看浏览器如何构建DOM树

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } p { font-size: 1 ...

  4. Chrome开发者工具详解(1)-Elements、Console、Sources面板

    Chrome开发者工具详解(1)-Elements.Console.Sources面板 Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Netwo ...

  5. Chrome开发者工具详解(1)

    Chrome开发者工具面板 面板上包含了Elements.Console.Sources.Network.Timeline.Profiles.Application.Security.Audits这些 ...

  6. Chrome 开发工具指南

    Chrome 开发工具指南 谷歌 Chrome 开发工具,是基于谷歌浏览器内含的一套网页制作和调试工具.开发者工具允许网页开发者深入浏览器和网页应用程序的内部.该工具可以有效地追踪布局问题,设置 Ja ...

  7. Chrome开发者工具详解(1):Elements、Console、Sources面板

    Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Network面板. Timeline面板.Profiles面板.Application面板.Se ...

  8. 详解Google Chrome浏览器(操作篇)(一)

    开篇概述 在上篇博客中详解Google Chrome浏览器(理论篇)一文中,主要讲解了Chrome 搜索引擎使用.Chrome安装和基本操作.Chrome 基本架构.多线程等原理性问题,这篇将重点讲解 ...

  9. Chrome DevTools 调研笔记

    1  说明 此篇文章针对Chrome DevTools常用功能进行调研分析.描述了每个功能点能实现的功能.应用场景和详细操作. 2  Elements 2.1  功能 检查和实时更新页面的HTML与C ...

随机推荐

  1. MMORPG大型游戏设计与开发(客户端架构 part7 of vegine)

    我在讲述某个东西的时候总喜欢从简单的入手,然后从互相关联的地方联合讲解,因为时间关系所以没能讲的十分详细,这点引以为憾,希望得到大家的谅解.这一节讲述的是微引擎(vengine)比较简单的一个模块,那 ...

  2. Sql动态添加字段的正确姿势

    如何给指定表动态添加字段? 一.创建一张表[Tbl_AutoFileds] (tableName表名,fieldName字段名,dataType数据类型,length长度  isnull 是否允许为n ...

  3. 《Invert》开发日志03:一些想法

    本来标题想写“详细设计”,但是由于独立游戏开发有很强的探索性,最终项目一定是经过原型调整迭代而来的,所以在实际效果出来之前把设计做得太细并没有太大意义,现在只能先陈列目前的一些想法,不能定义“它是什么 ...

  4. 三维网格分割算法(Random Walks)

    首先以一维随机游走(1D Random Walks)为例来介绍下随机游走(Random Walks)算法,如下图所示,从某点出发,随机向左右移动,向左和向右的概率相同,都为1/2,并且到达0点或N点则 ...

  5. Java中的四套读写方案

    一.字节流读写方案 FileInputStream:字节流方式读取文本文件 FileoutputStream:字节流写入硬盘 二.字符流读写方案 FileReader:字符流读取文本 FileWrit ...

  6. Win7虚拟机无法打开内核设备:\\Global\\vmx86

    Win7虚拟机无法打开内核设备:\\Global\\vmx86 听语音 | 浏览:16844 | 更新:2014-07-21 11:21 | 标签:虚拟机 1 2 3 4 5 6 7 分步阅读 一键约 ...

  7. hibernate考试题

    1.在Hibernate中,以下关于主键生成器说法错误的是(C). A.increment可以用于类型为long.short或byte的主键 long,short,byte都是特殊的int类型 B.i ...

  8. js+JQuery实现返回顶部功能

    很多网站上都有返回顶部的效果,本文阐述如何使用jquery实现返回顶部按钮. 首先需要在顶部添加如下html元素: <p id="back-to-top"><a  ...

  9. ajax跨域之设置Access-Control-Allow-Origin

    通过在服务器端设置请求头的源可以实现跨域 public function test_ajax() { header("Access-Control-Allow-Origin: http:// ...

  10. wget: unable to resolve host address的解决方法

    摘要:wget:无法解析主机地址.这就能看出是DNS解析的问题. wget:无法解析主机地址.这就能看出是DNS解析的问题. 解决办法: 登入root(VPS).进入/etc/resolv.conf. ...