javascript - D3 中 jQuery 的 $ (".cell:first") 是什么?

标签 javascript jquery dom d3.js

我试过了

d3.select(".cell:first")
d3.selectAll(".cell").filter(":first")
d3.selectAll(".cell").select(":first")

但都不行

最佳答案

d3.select(".cell")已经选择了第一个匹配的元素:

Selects the first element that matches the specified selector string, returning a single-element selection. If no elements in the current document match the specified selector, returns the empty selection. If multiple elements match the selector, only the first matching element (in document traversal order) will be selected.

来源:https://github.com/mbostock/d3/wiki/Selections#wiki-d3_select

“我如何获得最后一项?

D3 似乎返回 d3.selectAll() 的结果在集合中,位于数组中。例如,请求关于 d3 homepage 的所有段落结果:

[ Array[32] ] // An array with a single array child. Child has 32 paragraphs.

因此,如果我们想从该集合中获取最后一段,我们可以执行以下操作:

var paragraphs = d3.selectAll("p");
var lastParag  = paragraphs[0].pop();

或者更简洁:

var obj = d3.select( d3.selectAll("p")[0].pop() );

“:last-child 怎么样?”

:last-child选择器与获取页面上的最后一个元素不同。该选择器将为您提供作为其父容器的最后一个子元素的元素。考虑以下标记:

<div id="foo">
  <p>Hello</p>
  <p>World</p>
  <div>English</div>
</div>
<div id="bar">
  <p>Oie</p>
  <p>Mundo</p>
  <div>Portuguese</div>
</div>

在此示例中,运行 d3.select("p:last-child")不会返回您的任何段落。连d3.selectAll("p:last-child")惯于。这些容器都没有一个段落的最后一个子元素(它们是 <div> 元素:<div>English</div><div>Portuguese</div> )。

关于javascript - D3 中 jQuery 的 $ (".cell:first") 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10640430/

相关文章:

javascript - Angular2 RC-4 迁移因反射元数据而失败

jquery - 根据选项元素中的文本进行选项选择

jquery - 使用 jquery 从变量中删除 DOM 元素

javascript - 如何通过串联使用 createTextNode()

javascript - 如何在给定点直接或通过另一个元素的空腔检测哪个元素可见/位于顶部?

javascript - 错误 : read ECONNRESET at TLSWrap. onStreamRead

javascript - AngularJS 访问 ng-repeat 复选框

javascript - 错误: Can't set headers after they are sent.node.js

jquery - 完整日历中的resourceGroupField在 Angular 4中不起作用

javascript - 表单提交后如何清除输入字段