【问题标题】:Iterating through table cells using jQuery and appending to div使用 jQuery 遍历表格单元格并附加到 div
【发布时间】:2012-12-31 00:15:39
【问题描述】:

我在这里真的很困惑.. 我正在尝试从表中获取值并将这些值添加到具有类标题的 div 中 附加函数和.text 函数。 问题是,当我使用每个函数时,表中的最后一个值会被附加并且 其余的被跳过.. 这是我的代码:

<script type="text/javascript">
    $(document).ready(function() {
        $('#nameGridView tr').each(function() {
            if (!this.rowIndex) return; // skip first row
            var productName = this.cells[0].innerHTML;
            $('.title').each(function (i, obj) {
                $('.title').html(productName);

            });

        });
    });

</script>

请告诉我如何遍历每个表并同时添加,例如第一个 div 标题、表中的第一个值被附加等等。 谢谢。

【问题讨论】:

    标签: jquery html-table iteration


    【解决方案1】:

    使用append() 附加到元素。也可以使用.eq() 获取与行索引相同的div。

    <script type="text/javascript">
        $(document).ready(function() {
            var $title = $('.title');
            $('#nameGridView tr').each(function(i) {
                if (!this.rowIndex) return; // skip first row
                var productName = this.cells[0].innerHTML;
                $title.eq(i-1).append(productName);            
            });
        });
    
    </script>
    

    【讨论】:

    • 还有很多 div。与表格中要附加到的行单元格相同的 div...拜托!帮帮我..我真的需要这个...
    • 我希望我能在这里给你积分,但我是新手,我的积分很低,所以我不能,但一旦我有,我一定会这样做..:)
    • @RyanComputerProgrammer 表示感谢,您可以通过单击答案旁边的勾号来接受此答案。
    猜你喜欢
    • 2012-05-13
    • 2011-10-04
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    相关资源
    最近更新 更多