【问题标题】:jQuery: How can i filter Elements with a specific number of childrenjQuery:我如何过滤具有特定数量的子元素的元素
【发布时间】:2013-09-20 06:13:19
【问题描述】:

我有以下情况:

在我的网站上,我有一些架构几乎相同的元素。它们之间的唯一区别是孩子的数量。

我想获得具有类属性“XY”和两个孩子的元素。必须过滤掉具有三个子元素的元素。

如何使用 jQuery 做到这一点?

提前致谢。

编辑

这里有一些 HTML 给你!

这是我想要的元素之一:

<g class="gbox" rel="242">
    <path id="p012010-0" fill="#FFFFFF" stroke="#000000" stroke-width="0.850" class="stroke" d="M228.199,318.106 L259.401,318.106 L259.401,289.226 L228.199,289.226 L228.199,318.106  Z" style="fill: #ebe8c8;"></path>
    <text id="t012010-0" x="243.80" y="312.58" ix="228.62" iy="289.65" class="box" fill="#000000" editable="true" solution="5d">K</text>
</g>

这是一个必须过滤掉的:

<g class="gbox" rel="245">
    <path id="p015010-0" fill="#FFFFFF" stroke="#000000" stroke-width="0.850" class="stroke" d="M321.805,318.106 L424.772,318.106 L424.772,289.226 L321.805,289.226 L321.805,318.106  Z"></path>
    <g fill="#000000" stroke="none" transform="matrix(1.200 -0.000 -0.000 1.200 0 0)"><use xlink:href="#2ca9a4d0" x="273.741" y="256.625"></use>
        <use xlink:href="#2c9d4c70" x="281.147" y="256.625"></use>
        <use xlink:href="#2c9d4db8" x="288.928" y="256.625"></use>
        <use xlink:href="#2ca9cc88" x="294.866" y="256.625"></use>
    </g>
    <text id="t015010-0" x="373.29" y="312.58" ix="322.23" iy="289.65" class="box" fill="#000000" editable="true" solution="39"> </text>
</g>

【问题讨论】:

  • 你能放一些示例代码来详细说明这个问题吗?

标签: javascript jquery dom filtering


【解决方案1】:

使用.filter()

var $child2 = $('.XY').filter(function(){
    return $(this).children().length == 2
})

【讨论】:

  • 谢谢!这正是我正在寻找的,非常适合我。
猜你喜欢
  • 2013-09-06
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 2014-03-21
  • 1970-01-01
  • 2019-05-15
  • 2018-03-17
相关资源
最近更新 更多