jQuery 1.7.1:length函数无法获取表tr计数

时间:2012-03-23 20:50:24

标签: jquery html jquery-selectors

当我们升级到jQuery 1.7.1时,此代码停止工作。 $('table.className > tr').length正在返回0.你能告诉我如何纠正它吗?

.NET代码(执行jQuery代码):

public static long GetTableRowCountByCssClass(IWebDriver driver, string cssClass, int exclusionRowCount)
{
    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    long count = (long)js.ExecuteScript("return $('table." + cssClass + " > tr').length");
    if (count != 0)
    {
        count = count - exclusionRowCount;
    }
    return count;
}

HTML code:

<!-- Products -->
<table class="cart" cellpadding="0" cellspacing="0" border="0">

    <thead>
        <tr>
            <th class="remove">Delete</th>
            <th class="prod-desc">Product Description</th>
            <th class="ships_w">Ships Within</th>
            <th class="price">Unit Price</th>
            <th>&nbsp;</th>
            <th class="quantity">Qty.</th>
            <th class="totals">Total</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <td id="subtotal" class="order_summary_label" colspan="6" >Sub Total: </td>
            <td id="subtotal_number" class="order_summary_value">$124.89</td>
        </tr>
        <tr>
            <td id="cart-total-label" class="order_summary_label" colspan="6" >Total: </td>
            <td id="cart-total" class="order_summary_value"><strong>$124.89</strong></td>
        </tr>
    </tfoot>

    <tbody>

        <tr class="alt">

            <td class="remove">
                <input class="checkIt" type="checkbox" name="remove[variation][208]" id="remove_208_variation" value="0"  />
            </td>

            <td class="prod-desc">
            <div class="imgThmb">
            <a href="/product/water-gel" title="Water Gel - Magic Slush Powder - Water Gel 100 gram Jar">
            <img src="/img/cache/product/WSPA_500_140_64.jpg" alt="Water Gel - Magic Slush Powder - Water Gel 100 gram Jar" />
            </a>
            </div>

                <a href="/staging/product/water-gel" title="Water Gel - Magic Slush Powder - Water Gel 100 gram Jar" class="clean">
                    Water Gel - Magic Slush Powder - Water Gel 100 gram Jar                                                     
                </a>
            </td>

            <td class="ships_w">
            </td>

            <td class="price">$6.99</td>

            <td class="discount">
            </td>

            <td class="quantity">
                <input type="text" class="quantity_box quantity" name="quantities[variation][208]" id="quantity_variation_208" value="5" maxlength="4" />
            </td>

            <td class="totals"><strong>$34.95</strong></td>

        </tr>

        <tr >

            <td class="remove">
                <input class="checkIt" type="checkbox" name="remove[variation][1118]" id="remove_1118_variation" value="0"  />
            </td>

            <td class="prod-desc">
            <div class="imgThmb">
            <a href="/product/flying-film-canisters" title="Flying Film Canisters Kit - Flying Film Canisters Activity Kit">
            <img src="/img/cache/product/film_canister_launcher_2011011240_64.jpg" alt="Flying Film Canisters Kit - Flying Film Canisters Activity Kit" />
            </a>
            </div>

            <a href="/product/flying-film-canisters" title="Flying Film Canisters Kit - Flying Film Canisters Activity Kit" class="clean">
            Flying Film Canisters Kit - Flying Film Canisters Activity Kit                                                      
            </a>
            </td>

            <td class="ships_w">
            </td>

            <td class="price">$14.99</td>

            <td class="discount">
            </td>

            <td class="quantity">
                <input type="text" class="quantity_box quantity" name="quantities[variation][1118]" id="quantity_variation_1118" value="6" maxlength="4" />
            </td>

            <td class="totals"><strong>$89.94</strong></td>

        </tr>

    </tbody>

</table>

1 个答案:

答案 0 :(得分:8)

问题在于tr 不是 ,而且不能是table元素的直接子项。它是tbody元素的子元素,如果页面的作者没有将它放在table中,它将自动放在$('table.className > tbody > tr').length中。

如果您尝试,请改为:{{1}}它应该有效。