2012-05-23 63 views
2
<table border="2"> 
    <tbody> 
     <tr> 
      <td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td class="last">THIS SHOULD BE DELETED</td> 
     </tr> 
     <tr> 
      <td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td class="last">NON DELETED</td> 
     </tr> 
     <tr> 
      <td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td class="last">NON DELETED</td> 
     </tr> 
     <tr> 
      <td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td class="last">NON DELETED</td> 
     </tr> 
    </tbody> 
</table> 

http://jsfiddle.net/Vwa3p/如何刪除最後一個TD在第一線

如何刪除第一行最後一個TD與CSS或jQuery的? 應顯示:無,但如何?

+0

jQuery是很好,但總是試圖去如果可能的話CSS的解決方案。請閱讀下面的答案。 –

回答

2

使用jQuery:

$('.last:first').remove(); 

使用CSS:

table tbody tr:first-child td.last { 
    display:none 
} 

乾杯!只有

1

如果您需要將其刪除使用:

$("table tr:first > td:last").remove();​ 

如果你需要隱藏它使用:

$("table tr:first > td:last").hide();​ 

DEMO:http://jsfiddle.net/Vwa3p/1/

0
$('table > tbody > tr:first > td:last').hide() 

有了這樣的CSS :

table > tbody > tr:first-child > td:last-child 

我建議使用子選擇器(>)來加快遍歷並將其限制爲僅在tbody中搜索,您永遠不知道您以後是否會廣告thead和/或tfooter。

+0

-1你真的不應該鼓勵用戶依靠javascripts自動插入''''。我開始變得非常惱火。 – Christoph

+0

哈哈,你就像5歲?哦,好的...並且沒有理由使用;在一行代碼:)同樣的事情與CSS,最後的規則不一定有一個; – tbleckert

+0

嗯,我可以在你的陳述中證明你是錯的,但我把這留給你看看。 – Christoph

2

第一行,意思是第一行?

$('tr:first td:last').hide(); 
+0

這樣會很慢,搜索整個頁面而不是僅在表格中搜索並不好。此外,如果它是CSS,它將會更慢,因爲css從右到左遍歷... – tbleckert

+1

它會變慢,但可以忽略單點操作。如果這個操作是沒有變量緩存的艱苦重複,那麼這就是另一個不同的調用。放置一個「table」上下文的真正好處是限制DOM搜索,因爲例如,你不想隱藏所有'tr'中所有'tr'中的所有'td'表在文件中。 –

1

CSS:

​table tr:first-child td:last-child{ 
    display:none; 
}​ 

​table tr:first-child .last{ 
    display:none; 
}​ 
+0

如前所述,css從右向左搜索,這意味着它將首先搜索整個頁面,以查找每個td:last-child,每個tr的每個最後一個td的查找(在搜索完整個文檔之前不會停止)。那麼最後呢,它在桌子上嗎?更智能將 表> tbody> tr> td:最後一個孩子 – tbleckert

+0

@tbleckert 1.我知道CSS如何工作。 2.確定你可以做一些優化,但這不是問題的一部分。我只是展示了一個工作解決方案沒有理由downvote imo ... 3.在一個有點正常的文件,這將不會明顯無論如何。 – Christoph

+0

對不起。但是我開始對這裏的用戶非常惱火。你沒有幫助任何人提供「工作」解決方案,教學和解釋至少與解決方案本身一樣重要。 – tbleckert

相關問題