如何使用jQuery迭代HTML页面中每个表的所有表行

时间:2022-03-02 19:18:14

I need the correct syntax to write two loops inside each other. First one to loop over each table in an html page with no ids or classes and the second one to iterate over each table row of the table specified by the first loop in jQuery.

我需要正确的语法来在彼此内部编写两个循环。第一个在没有id或类的html页面中遍历每个表,第二个遍历jQuery中第一个循环指定的表的每个表行。

Here is my jQuery but its not working probably wrong syntax.

这是我的jQuery,但它不起作用可能是错误的语法。

$(document).ready(function(){
    $('table.rep').each(function(){
        $(this + ' tr').each(function{
            // change style of this tr
        });
    });
 });

3 个解决方案

#1


0  

You do it like this (uses the jQuery context parameter to restrict the search for a tr element to the table:

你这样做(使用jQuery上下文参数来限制搜索表中的tr元素:

$(document).ready(function(){
    $('table.rep').each(function(){
        $('tr', this).each(function{
             // change style of this tr
        });
    });
});

Or alternatively, like this (uses the find method to find elements that reside inside the element represented by the jQuery object you call the method on):

或者,像这样(使用find方法查找驻留在由jQuery对象表示的元素内的元素,并调用方法):

$(document).ready(function(){
    $('table.rep').each(function(){
        $(this).find('tr').each(function{
             // change style of this tr
        });
    });
});

You don't even have to nest loops depending on what you want to do, just looping over all table rows may suffice:

您根本不需要嵌套循环,这取决于您想要做什么,只需循环遍历所有表行就足够了:

$('table tr').each(function(){
    // change tr style
});

#2


0  

Or this:

或这个:

$('table.rep tr').each(function(){
   ...        
});

#3


0  

<script>
    $(document).ready(function () {
        $('table.rep').each(function () {
            $(this).find('tr').each(function () {
            // Do your stuff
            });
        });
    });

 </script>

#1


0  

You do it like this (uses the jQuery context parameter to restrict the search for a tr element to the table:

你这样做(使用jQuery上下文参数来限制搜索表中的tr元素:

$(document).ready(function(){
    $('table.rep').each(function(){
        $('tr', this).each(function{
             // change style of this tr
        });
    });
});

Or alternatively, like this (uses the find method to find elements that reside inside the element represented by the jQuery object you call the method on):

或者,像这样(使用find方法查找驻留在由jQuery对象表示的元素内的元素,并调用方法):

$(document).ready(function(){
    $('table.rep').each(function(){
        $(this).find('tr').each(function{
             // change style of this tr
        });
    });
});

You don't even have to nest loops depending on what you want to do, just looping over all table rows may suffice:

您根本不需要嵌套循环,这取决于您想要做什么,只需循环遍历所有表行就足够了:

$('table tr').each(function(){
    // change tr style
});

#2


0  

Or this:

或这个:

$('table.rep tr').each(function(){
   ...        
});

#3


0  

<script>
    $(document).ready(function () {
        $('table.rep').each(function () {
            $(this).find('tr').each(function () {
            // Do your stuff
            });
        });
    });

 </script>