【问题标题】:How would I go about appending to a table row from within a jquery each function我将如何从 jquery 每个函数中附加到表行
【发布时间】:2009-07-02 21:22:43
【问题描述】:

在下面的代码中,$j 是 jquery 对象。我想知道为什么我可以很好地附加到 tbody 标签,但是我不能通过以下任一选择器添加到 tr 标签:tr tbody 或 tr。如何正确附加到表格行?感谢您的帮助。

var $this = $j('<table><thead></thead><tbody><tr></tr></tbody></table>');
$j.each(settings.columns, function(i, val) {
        $j('<td></td>').appendTo($this.children('tbody tr')); 
        // Doesnt work with tr selector either, but works with tbody
        // More code
}

【问题讨论】:

    标签: jquery jquery-plugins


    【解决方案1】:

    children 只找到直系子代(这就是为什么 &lt;tbody&gt; 有效),否则你需要 find

    $j('<td></td>').appendTo($this.find('tbody tr')); 
    

    您也可以使用$this 作为查询的context

    $j('<td></td>').appendTo($j('tbody tr', $this)); 
    

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 2012-06-05
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多