【问题标题】:Find the nth div with a certain class找到某个类的第 n 个 div
【发布时间】:2014-10-23 23:18:02
【问题描述】:

这也应该适用于 IE8

我的 tr rows 元素有多个 td 元素,每个 td 包含多个 div。一些 div 具有“cell-data”类

在一排的所有这些 div 中。我正在尝试使用“cell-data”类查找和操作 second 和 third div

我可以正确获得第一个孩子,但不能正确获得第三个孩子。我在这里搜索 SO 也试过 eq() 方法但还是找不到。

$( ".rows" ).each(function() {
    var height = $(this).find('.cell-data:nth-child(1)').css("height");
    var height3= $(this).find('.cell-data:nth-child(3)').css("height") ;
});

而 Html 看起来像这样

                  <tr class="rows">
                                    <td class="cell-td">  
                                        <div class="row">
                                            <div class="col-3">
                                                <div>
                                                </div>
                                            </div>
                                            <div class="col-3">
                                                <div  class="cell-data">

                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="cell-td">

                                            <div class="cell-data" >

                                                </span>
                                            </div>


                                    </td>
                                    <td class="cell-td">
                                        <div class="cell-data">

                                        </div>
                                    </td>
                                </tr>

【问题讨论】:

  • 你也可以粘贴 HTML 吗?
  • 你能对剩下的代码做个小技巧吗?
  • 请记住,nth-child() 不适用于某些版本的 IE
  • @Spring 向我们展示您的标记。
  • 它不适用于 CSS,但这是 jQuery 选择器 - jQuery 实现了自己的搜索,所以它应该可以工作。

标签: javascript jquery html


【解决方案1】:

.cell-data:nth-child(3) 不会为您提供该类的第三个 div。它给了你父母的第三个孩子。

请改用.cell-data:nth-of-type(3)

【讨论】:

  • tnx 但仍未定义
【解决方案2】:

由于@Vld 建议的问题是由于 td 元素,所以我修复了;

$(".rows").each(function () {
        var td1 = $(this).find('.cell-td:nth-of-type(1)');
        var td2 = $(this).find('.cell-td:nth-of-type(2)');
        var td3 = $(this).find('.cell-td:nth-of-type(3)');

        var height = $(td1).find('.cell-data:nth-of-type(1)').css("height");
        $(td2).find('.cell-data:nth-of-type(1)').css("height", height);
        $(td3).find('.cell-data:nth-of-type(1)').css("height", height);
    });

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 2013-01-22
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 2012-11-18
    • 2017-02-19
    • 2021-04-09
    • 2015-02-15
    相关资源
    最近更新 更多