使用jQuery迭代并删除空的HTML表行
我正在尝试使用jQuery遍历HTML表并删除空行.该页面由ASP.NET驱动,并具有一定的权限,该表中的项目将被隐藏.因此,我想创建此脚本以删除空行并消除仍然显示的其他项目之间的空间.我似乎无法获得当前必须运行的功能,而且不确定为什么.这是代码:
I am attempting to iterate through a HTML table using jQuery and delete empty rows. The page is driven by ASP.NET and with certain permission, items in the table will be hidden. Therefore, I wanted to create this script to remove the empty rows and get rid of the space between the other items that are still displayed. I cannot seem to get what I currently have to run and I am unsure as to why. Here is the code:
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('tr').each(function () {
$(this).find('td').each(function () {
if ($(this).text().trim() == "") {
$(this).closest("tr").remove();
};
});
});
});
</script>
任何指导将不胜感激.谢谢!
Any guidance would be greatly appreciated. Thanks!
您的代码似乎正常运行.尝试单独运行以下命令以了解我的意思:
Your code appears to work just fine. Try running the following on its own to see what I mean:
<html>
<head>
<title>jQuery Tests</title>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('tr').each(function () {
$(this).find('td').each(function () {
if ($(this).text().trim() == "") {
$(this).closest("tr").remove();
};
});
});
});
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td></td>
<td>testing</td>
</tr>
<tr>
<td>testing</td>
<td>testing</td>
</tr>
<tr>
<td>testing</td>
<td> </td>
</tr>
</table>
</body>
页面上是否还有其他可能冲突的东西?
Could there be something else on the page that might be conflicting?