使用jQuery迭代并删除空的HTML表行 [英] Iterate through and delete empty HTML table rows with jQuery

查看:100
本文介绍了使用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?

这篇关于使用jQuery迭代并删除空的HTML表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆