简体   繁体   English

在 Chrome 控制台中选择 DOM 元素

[英]Selecting DOM elements in Chrome console

I'm a bit puzzled by the following: Let's say I've got a paragraph element with the id of para .我对以下内容感到有些困惑:假设我有一个段落元素,其 ID 为para Using Chromes console, if I say使用 Chromes 控制台,如果我说

document.getElementById("para")

I'm returned with the HTML snippet <p id="para">....</p> , whereas if I use for instance the Javascript library D3's selection method and say我返回 HTML 代码段<p id="para">....</p> ,而如果我使用例如 Javascript 库 D3 的选择方法并说

d3.select("#para")

I'm returned with the DOM node and can access all the properties and methods of the paragraph element.我返回了 DOM 节点,可以访问段落元素的所有属性和方法。

Why this difference?为什么会有这种差异?

By default, when logging a DOM node in Chrome, it displays as markup.默认情况下,在 Chrome 中记录 DOM 节点时,它显示为标记。 To log the DOM node as a normal object use console.dir .要将 DOM 节点记录为普通对象,请使用console.dir The reason d3.select("#para") shows as a normal object is that this method probably doesn't return a DOM node, but an object that wraps over the DOM node. d3.select("#para")显示为普通对象的原因是该方法可能不会返回一个 DOM 节点,而是一个包装在 DOM 节点上的对象。

console.dir(document.getElementById("para"));

The best method in my opinion is this:我认为最好的方法是这样的:

  • open the chrome console打开 Chrome 控制台
  • type: $x("//input[@id='para']")类型: $x("//input[@id='para']")

With a click on the found element you can also see it selected单击找到的元素,您还可以看到它被选中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM