2012-11-06 57 views
0
<table> 
<tr id="Name"></tr> 
<tr id="Name"></tr> 
<tr id="Name"></tr> 
<tr id="Name"></tr> 
<tr id="Name"></tr> 
<tr id="address"></tr> 
</table> 

這是我的問題。我想刪除所有tr,其idName而不使用任何循環。刪除全部相同的ID在表

這裏是我的代碼

jQuery('#Name').remove(); 
+4

ID應該是唯一的 – Sibu

+0

'Id'應該是唯一的。如果您提出的摘錄不起作用,這就是原因。使用課程,例如''。 – skalee

+0

是的,我知道ids應該是唯一的,但這是問題 –

回答

2

Yopu更好用一些類而不是ID作爲ID應該是在DOM元素獨特。

Live Demo

$('.someclass').remove(); 
1

ID都是唯一的,不能使用同一ID兩次,但是你可以使用同一個類 'n' 的次數

<table> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr id="address"></tr> 
</table> 

<script> 
    jQuery('.Name').remove(); 
</script> 
2

不要使用ID屬性,如果它不是一個標識符!一個標識符在整個文檔中必須是唯一的,否則你會得到像你一樣的錯誤。

如果多個元素應該共享某些屬性(如你的情況是可選擇的一起),使用其他屬性,像data-name(有時)或class

在你的情況下,你想使用名稱或類屬性。如果你決定使用class屬性時,JS可能看起來像以下:

jQuery('.Name').remove(); 

有了這個HTML

<table> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="Name"></tr> 
<tr class="address"></tr> 
</table> 
1

使用jQuery的屬性選擇器尋找元素:

唐在同一文檔中不能使用多個元素的id。

$("tr[id='Name']").remove() 

演示在這裏:jsfiddle