Lets say that I have a very simple table:
<table>
<tr>
<td>TextFirst</td>
<td>TextSecond</td>
</tr>
</table>开发者_JAVA技巧
How can I traverse the table and remove "TextSecond". The table could be any number of rows or cells.
In your posted example you have, at least, two options I can think of:
$('td:nth-child(2)').remove();
Or:
$('tr td:eq(1)').remove(); // zero-based index, thanks @ZDYN for the catch
Or, in plain JavaScript:
var removeThis = document.getElementsByTagName('td')[1];
removeThis.parentNode.removeChild(removeThis);
JS Fiddle demo.
$("table td:nth-child(2)").remove()
//var table = document.getElementById("?");
for (i=0; i < table.rows.length; i++)
{
if (table.rows[i].cells.length > 1)
table.rows[i].cells[1].removeNode(true);
}
or if you want to delete cells based on some condition. just because
//var table = document.getElementById("?");
for (i=0; i < table.rows.length; i++)
{
for (j = table.rows[i].cells.length - 1; j >= 0; j--)
{
if (table.rows[i].cells[j].innerHTML == 'TextSecond')
table.rows[i].cells[j].removeNode(true);
}
}
You can use nth-child
... :
$('table :nth-child(2)').remove();
JSFIDDLE
精彩评论