admin管理员组文章数量:1325380
How can i remove entire <tr>
from a table using javascript without using getElementByID
because i do not have any IDs of table tr
or tds
How can i remove entire <tr>
from a table using javascript without using getElementByID
because i do not have any IDs of table tr
or tds
- 4 Could you read that sentence back and reword it so it makes sense? Also, it'd be handy if you could post the code you have tried. We're not here to do your work for you! – Alex Commented May 3, 2012 at 10:50
- yes Alex im also not even hiring you for my work, i have done my part and requested people, if u know about this your suggestion are highly appreciable otherwise i have not called you here – Arun Kumar Commented May 3, 2012 at 11:24
- You've given us no context to work with! I had to read through to the bottom to realise that some click event was what's triggering this. Please try to give AS MUCH detail as possible in your question. We're not mind readers – Alex Commented May 3, 2012 at 11:28
4 Answers
Reset to default 5Assume your table has id="mytable":
This is how you get all the trs:
1. var trs = document.getElementById("mytable").rows; or
2. var trs = document.getElementById("mytable").getElementsByTagName("tr");
Now do this:
while(trs.length>0) trs[0].parentNode.removeChild(trs[0]);
If you don't have id for your table and you have only one table on the page, do this:
var trs = document.getElementsByTagName("table")[0].rows;
It would be hard to remove a specific tr if you don't give it an id. If you want to remove certain kind of trs, give them a class attribute and remove only those trs with this class attribute.
You could try something like this with jQuery:
$(function(){
$('tr, td').remove();
});
Or if — for whatever reason — you'd like to remove all contents of a table:
$('table').empty();
Once you have identified the <tr>
you wish to remove (you haven't provided enough context to your question to suggest (without making lots of assumptions) how you might do that):
tr_element.parentNode.removeChild(tr_element)
try something like this
<html>
<head>
<style>
</style>
<script src="http://code.jquery./jquery-latest.js"></script>
</head>
<body>
<table>
<tr>
<td>test</td><td>test</td><td>test</td>
</tr>
<tr>
<td>test</td><td>test</td><td>test</td>
</tr>
</table>
<button>Empty</button>
<script>
$("button").click(function () {
$("table").empty();
});
</script>
</body>
</html>
本文标签: htmlRemoving lttrgt using javascriptStack Overflow
版权声明:本文标题:html - Removing <tr> using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192285a2430435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论