2014-07-23 38 views
0

我試着去一個type=checkbox追加到最後tdtr添加類型的DOM元素使用jQuery

我一直在實現這一穿越的想法玩弄,但我不知道它是否能正常工作。沿着這條線的東西。

$('#myID tr').children("td:nth-last-of-type(1)").addClass("me"); 

我知道這是添加類的元素,但它可以以類似的方式進行添加TYPEtr DOM元素? 有一個或兩個例子,如this其中一個,但它的所有變化類型,而不是添加一個新的。

<table id="myID" class="myClass"> 
    <tr> 
     <th>one</th> 
     <th>two</th> 
     <th>three</th> 
     <th>four</th> 
    </tr> 
    <tr> 
     <td>1</td> 
     <td>2</td> 
     <td>3</td> 
     <td>4</td> 
    </tr> 
    <tr> 
     <td>a</td> 
     <td>b</td> 
     <td>c</td> 
     <td type='checkbox'></td> <!-- **THIS IS WAHT IM AFTER** --> 
    </tr> 
</table> 

<input type='button' id='but_id' value='Click Me'/> 
+0

它的設置,不改變,它應該工作...你已經嘗試過了嗎? (在鏈接中討論示例) –

+1

使用'.attr(「type」,'checkbox')'而不是'.addClass(「me」)' –

+0

'我不認爲'​​'元素意味着有'類型= 「複選框」'? –

回答

1

不能添加到type="checkbox TD,這是無用的,無效的,但你可以添加複選框td例如

$('#myID tr').children("td:nth-last-of-type(1)").append('<input type="checkbox" />'); 
1

您不能在td中添加type = 'checkbox'它是無效的。只是追加在TD輸入型

$('#myID tr').find("td").last().append("<input type='checkbox' />"); 

埃弗特最後TD意味着

$('#myID tr').find("td:nth-last-child(1)").append("<input type='checkbox' />"); 

DEMO

0

添加type = 'checkbox'並不意味着什麼,它的錯誤HTML。你可以在td中添加複選框。

$('#myID tr').children("td:last-child").append('<input type="checkbox" />'); 

DEMO