2014-06-09 37 views
0

我想從第15個td列中獲取div,並將div的寬度設置爲300px。使用jquery在td中找到div

我有表格摘要來識別表格。如何設置寬度?

<table summary="Copy and Design RFP"> 
.... 
... 
/* column15*/ 
<td class="ms-vb2"> 
    <div dir="">1/9/14 - 1st draft sent</div> 
</td> 

到目前爲止,我寫了

$(document).ready(function() { 
     if (window.location.href.indexOf("dept/mkt/Lists/Request for Projects/Active Projects.aspx") > -1) { 
      $('table[summary="Copy and Design RFP"] tr td:nth-child(15)').each(function() { 
       var id = $(this).find('div'); // This is not working 
      }); 
     } 
    }); 
+0

'$( '表[摘要= 「複製與設計RFP」] TD:EQ(14)的div')寬(300)' – adeneo

回答

2

使用nth-child選擇:

$('table[summary="Copy and Design RFP"] td:nth-child(15) div').css('width', '300px'); 
+0

你必須'您選擇metacharacters'說。可能需要避開。 – PeterKA

+0

@ user3558931哪個?... – War10ck

+0

也許你可以告訴我們爲什麼你的版本可以工作,而OP的不是,因爲你使用的是幾乎相同的選擇器? – adeneo

0

我創建這個解決方案:

JS

var i = 1; 
$("table tr").children().each(function(){ 
    if(i==15){ 
    var div = $(this).find("div"); 
    $(div).attr("style","height:300px;"); 
    } 
    i++; 
}); 

fiddle