2012-03-23 19 views
1

當我們升級到jQuery 1.7.1時,此代碼停止工作。 $('table.className > tr').length正在返回0.您能告訴我如何糾正它嗎?jQuery 1.7.1:長度函數無法獲取表tr計數

.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代碼:

<!-- 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

你的意思是它在以前的版本? – BoltClock 2012-03-23 20:55:47

+0

是的,絕對!我們從1.5.1升級。 – MacGyver 2012-03-23 21:02:55

回答

7

的問題是,所述tr,且不能,這是table元素的直接子元素。它是tbody元素的一個子元素,如果它沒有放在頁面的作者那裏,它會自動放置在瀏覽器的table中。

如果你嘗試,而是:$('table.className > tbody > tr').length它應該工作。

+0

較少的代碼,但不太具體:$('table.cart tr')。length – 2012-03-23 20:56:18

+0

@David Thomas,假設你是對的,我剛學到了一些關於瀏覽器自動放置'tbody'的信息,非常感謝! – Marc 2012-03-23 20:57:34

+0

我不能說IE(愛好者,\ * nix-runner ...),但是我爲此行爲而故意查找的每個瀏覽器(Ubuntu上的Chromium,Firefox,Opera)都已經證明了這種行爲。 – 2012-03-23 20:58:54