2

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

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

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

3 回答 3

0

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

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

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

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

您甚至不必根据要执行的操作来嵌套循环,只需遍历所有表行就足够了:

$('table tr').each(function(){
    // change tr style
});
于 2012-09-24T11:42:03.397 回答
0

或这个:

$('table.rep tr').each(function(){
   ...        
});
于 2012-09-24T11:43:38.047 回答
0
<script>
    $(document).ready(function () {
        $('table.rep').each(function () {
            $(this).find('tr').each(function () {
            // Do your stuff
            });
        });
    });

 </script>
于 2012-09-24T11:53:51.943 回答