jQuery-设置TBODY [英] JQuery - Set TBODY

查看:561
本文介绍了jQuery-设置TBODY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义如下的表:

I have a table defined as follows:

<table id="myTable" cellpadding="0" cellspacing="0">
  <thead><tr>
    <th>Date</th>
    <th>First Name</th>
    <th>Last Name</th>
  </tr></thead>

  <tbody>
    <!-- rows will go here -->
  </tbody>
</table> 

我正在尝试在运行时通过JavaScript动态填充"myTable".为了适应这一点,我正在使用JQuery.我想在"myTable"中的tbody元素中写入一些HTML.但是,我在理解如何使用选择器进行操作时遇到了问题.我知道我可以使用以下命令获取"myTable":

I am trying to dynamically populate 'myTable' at runtime via JavaScript. To accomodate for this, I am using JQuery. I want to write some HTML into the tbody element within 'myTable'. However, I am having problems understanding how to do this with the selectors. I know that I can get 'myTable' using:

$("#myTable")

我知道我可以使用以下方法设置myTable的HTML:

I know that I can set the HTML of myTable by using the following:

$("#myTable").html(someHtmlString);

但是,这将设置整个表的HTML.实际上,我只想在"myTable"的TBODY中设置HTML.我该如何使用JQuery?

However, that sets the HTML of the entire table. In reality, I just want to set the HTML within the TBODY of 'myTable'. How do I do this with JQuery?

谢谢!

推荐答案

您将使用:

$("#myTable > tbody");

选择tbody的直接后代tbody元素.

或者,您可以使用:

$('tbody', '#myTable');

查找#myTable上下文中的所有tbody元素.

which finds all tbody elements within the context of #myTable.

在jQuery中,通常有几种方法可以满足您的需求.

In jQuery, there are often several ways to accomplish what you need.

另一种方法是:

$('#myTable').children('tbody');

实际上与我上面的第一个解决方案相同.

which is effectively the same as my first solution above.

jQuery具有出色的文档:

jQuery has great docs:

选择器: http://api.jquery.com/category/选择器/

遍历:: http://api.jquery.com/category/遍历/

这篇关于jQuery-设置TBODY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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