如何访问 thead 元素的子行?
我是 Javascript 新手,所以我不确定我是否只是没有理解它,或者我是否发现了某个错误......某些东西,尽管我不知道是什么。我有一个脚本正在加载数据库查询的结果,然后创建一个图形(使用 d3库),然后将原始数据吐出到表中。到目前为止,这一切都运行良好。
接下来我想做的是启用图表上各个数据线的切换。我不想进行 Ajax 调用以返回服务器并再次获取和解析数据,而是只想从原始数字表中读回数据,然后重新绘制图表 - 所有客户端。我的问题是尝试从 和
元素获取列名称值。我可以得到这个:
tbody = $("#raw_body");
console.log(tbody);
并在 Firebug 控制台中看到:
[tbody#raw_body]
在控制台中,我可以单击对象并看到 [0] 实例有“子项”等。但是,每次我尝试这样的操作:
console.log(tbody[0].children[0]);
我得到:
undefined
那么,我如何处理表中的行以分配给 Javascript 对象,就像这样?
r = tbody[0].children[0];
I'm new to Javascript, so I'm not sure if I'm just not getting it, or if I've found a bug in... something, though I wouldn't know what. I have a script that is loading the results of a database query, then creating a graph (with the d3 library), and then spitting out the raw data in a table. So far, this all works great.
What I would like to do next is enable the toggling of individual data lines on the graph. Rather than make an Ajax call to go back to the server and get and parse the data again, I'd like to just read the data back from the table of raw numbers, and redraw the graph -- all client side. My problem is trying to get the column names values from the <thead>
and <tbody>
elements. I can get this:
tbody = $("#raw_body");
console.log(tbody);
And see in the Firebug console:
[tbody#raw_body]
In the console, I can click through the object and see that the [0] instance has "children", etc. However, every time I try something like this:
console.log(tbody[0].children[0]);
I get:
undefined
So how do I address rows of the table for assigning to Javascript objects, like this?
r = tbody[0].children[0];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$("#raw_body tbody tr")
将为您提供表中 id 为 raw_body 的所有行。我不确定你的意思:“那么我如何寻址表的行以分配给Javascript对象,就像这样?”
$("#raw_body tbody tr")
will give you all the rows in the table with an id of raw_body.I'm not sure what you mean by: "So how do I address rows of the table for assigning to Javascript objects, like this?"
table
对象有一个名为rows
的属性。您可以使用它来循环遍历行。http://www.javascriptkit.com/domref/tableproperties.shtml
如果你想使用 jQuery:
table
objects have a property calledrows
. You can use that instead to loop through the rows.http://www.javascriptkit.com/domref/tableproperties.shtml
If you want to use jQuery:
它实际上是
.childNodes[0]
。 .children() 是一个 jquery 方法,它扫描子节点以查找指定的模式,因此请使用它。
It's actually
.childNodes[0]
. .children() is a jquery method that scans child nodes for a specified pattern, so useinstead.
返回底层 DOM 对象,而不是 jQuery 包装器。 DOM 对象没有
children
方法 (*)。如果您想要 jQuery 包装的版本,请使用eq()
代替:但是您可以省略
$('#raw_body' 上的
因为只有一个元素与.eq(0)
)#raw_body
匹配。 jQuery 包装器对元素和元素列表的处理方式相同,因此您无需提取单个元素即可对其调用children()
。*:尽管它们确实有一个
childNodes
属性。您可能不想使用它,因为这样您就必须担心元素之间的空白的文本节点。但是,表格尤其具有更易于使用的rows
和cells
属性,因此您可以使用以下方法来实现:Returns the underlying DOM object, not the jQuery wrapper. DOM objects don't have a
children
method (*). If you want the jQuery-wrapped version, useeq()
instead:However you can omit the
.eq(0)
on the$('#raw_body')
because there will only ever be one element that matches#raw_body
. jQuery wrappers treat an element and a list of elements the same so you don't need to extract a single element to be able to callchildren()
on it.*: though they do have a
childNodes
property. You probably don't want to use that because then you have to worry about the Text nodes for whitespace in between the elements. However, tables in particular haverows
andcells
properties which are easier to use, so you could do it with: