简体   繁体   English

选择DOM元素,但不选择D3中的子元素

[英]Selecting DOM elements but not their children in D3

I'm working with SCXML, and my data is something like this: 我正在使用SCXML,我的数据是这样的:

<state id="umbrella_state">
  <state id="state1"></state>
  <state id="state2>
    <transition event="cancel"></transition>
    <transition event="next"></transition>
  </state>
  <transition event="quit"></transition>
</state>

I'm using D3 for the purpose of visualizing a state and its transitions, but I'm struggling to accurately select just the desired transitions. 我使用D3的目的是可视化状态及其转换,但我一直在努力准确地仅选择所需的转换。

d3.selectAll("#transitions") // selects everything, which I don't want

What I want is to select only transitions for a state and not its substates. 我想要的只是选择一个状态的过渡,而不是其子状态。 For example, the only transition for state1 is "quit". 例如,状态1的唯一转换是“退出”。 I imagine something like: 我想像这样:

d3.selectAll("[id=umbrella_state]").selectAll("transition :not(transition > transition)")

(and repeat this for each parent state until I reach my desired state). (并针对每个父状态重复此操作,直到我达到所需的状态)。

According to the API docs select should only be getting the first element that matches it's selector condition not every element. 根据API文档, select应该只获取与其选择器条件匹配的第一个元素,而不是每个元素。

Also, according to the API docs, d3 selectors use the CSS3 standard so something like this 另外,根据API文档,d3选择器使用CSS3标准,因此类似

d3.select("state ~ transition")

Should only select the first transition sibling after a state element. 应该只选择状态元素之后的第一个过渡同级。 (the ~ character designates a sibling that follows at some point after the first selector, more here ) Unfortunately, there is no CSS3 selector for siblings that come before one. ~字符表示在第一个选择器之后的某个位置出现的兄弟姐妹,更多信息在此处 )不幸的是,没有CSS3选择器用于在兄弟姐妹之前的兄弟姐妹。

Hope this helps. 希望这可以帮助。

I resolved my issue with a selector like this: 我用这样的选择器解决了我的问题:

d3.selectAll("[id='" + state.attr("id") + "'] > transition")

Which translates to something like: 相当于:

d3.selectAll("[id='umbrella_state'] > transition")

I did that recursively for each level until I reached the deepest state. 我递归地执行每个级别的操作,直到达到最深的状态。

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

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