2013-10-20 46 views
0

鑑於以下html渲染;jQuery訪問動態類選擇器

<fieldset id="fld_Rye"> 
<legend>7 Main St.</legend> 
<div> 
    <table> 
     <tr> 
     <th class="wideCol"><b><i>Service Description</i></b></th> 
     <th class="wideCol"><b><i>Service Name</i></b></th> 
     <th class="normalPlusWidth"><b><i>Contact Name</i></b></th>   
    </tr> 
    <ItemTemplate> 
     <tr class=""> 
     <td class="servDesc">&nbsp;<b>Plumbing Services</b></td> 
        <td class="servName">&nbsp;<b>Flynnsters's Plumbing</b></td> 
        <td class="servContact">&nbsp;<b>Jim Flynnster</b></td> 
     </tr> 
    </ItemTemplate> 

我試圖訪問所有的TD與一類servDesc的,得到他們的寬度到一個數組中,並從該陣列得到的最大寬度重置使用在頁面加載jQuery的那個類的CSS寬度。我似乎無法爲這些標籤獲得正確的選擇,並且我嘗試了至少50個變體。

我最近的嘗試;

var maxTdWidth; 
     var servDescCols = []; 
     $("#fld_Rye td#servDesc").each(function() { 
      alert("found one"); 
      servDescCols.push($(this).width()); 
     }); 
     maxTdWidth = Math.max.apply(Math, servDescCols); 

回答

0

得到,如果你想針對任何元素,並得到內部使用這個

「表1」的TD所有tdservDesc類試試這個

$("td.servDesc").each(function() { 
      alert("found one"); 
      servDescCols.push(this.width()); 
    }); 

是的標識基本元素

$("#table1").find("td.servDesc").each(function() { 
     alert("found one"); 
     servDescCols.push(this.width()); 
}); 
+0

是的,我知道這樣的作品,但這個網頁可能比一個表更多,我希望在選擇器中有更多的層次結構,即定義div和sub td.servDesc,而不僅僅是任何td.servDesc標籤,當它們在頁面上是多個時,它也只會找到一個 – dinotom

+0

我已更新了我的答案。 –

+0

這的確行得通,但是那個選擇器有17個td標籤,它只能找到一個,或者更好一點,推送功能不起作用,腳本在第一次出現後退出。我也改變了這個$(this) – dinotom