2012-06-29 96 views
2

任何人都可以幫助我。我有一個表在其中一個單元格中包含嵌套表。我想追加「你好世界!」到外表的每一行中的第一個單元格(所以在下面的html中,我只想追加到單元格1a和2a)。我試過如下:JQuery - 直接子女的直接子女

$("div > table > tr > td:first-child").append(' Hello World!'); 

在下面的html:

<div class="divClass"> 

    <table class="tableClass" border="1px"> 
     <tr> 
      <td>1a</td> 
      <td>1b</td> 
      </tr> 
    <tr> 
      <td>2a</td> 
      <td>2b 
       <table border="1px"><tr><td>nested table cell</td></tr></table> 

      </td> 
    </tr> 
    </table> 

</div>​ 

,但它不工作。 ( - http://jsfiddle.net/NickyW/x3bqf/

有什麼建議嗎?

感謝,

尼克

回答

5
$("div > table > tbody > tr > td:first-child").append(' Hello World!'); 

您只需要使用tbody,因爲它通過瀏覽器添加到表中。這更可靠。

DEMO

+0

@Nick檢查並演示 – thecodeparadox

+0

+1以追加到正確的單元格和工作演示。 – Nope

+0

+1不錯的答案 - 我不會想到tbody :) – Dipak

-1

你是什麼意思與追加?你想將單元格的值設置爲Hello world?然後你應該用$(..)。html('Hello World')來試試它。 。

如果宥要追加到1A爲examle:

與$試試看(..)HTML($(..)HTML()+ 「Hello World」 的。);

+0

使用jQuery'的append()'要追加,而不是concatonating多個'HTML()'調用,這是它適用於。 – Nope

0

這很簡單,你需要做的就是添加TBODY,沒有必要引用div和請使用類引用。

$(function() { 

     $(".tableClass > tbody > tr > td:first-child").append(' Hello World!'); 

    }); 

Solution