jquery选择器迭代子级,然后迭代父级/同级
我有以下简化的 HTML。
<div id="coat">
<span class="Jan2011-Sales">10</span>
<span class="Feb2011-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="boot">
<span class="Jan2011-Sales">10</span>
<span class="Feb2011-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="hat">
<span class="Jan2011-Sales">10</span>
<span class="Feb-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="etc.">
</div>
编辑:
我想做的是创建一个如下表:(几乎像数据透视表)
<th>
<td>Period</td>
<td>Coat</td>
<td>Boot</td>
<td>Hat</td>
</th>
<tr>
<td>Jan2011-Sales</td>
<td>10</td>
<td>10</td>
<td>10</td>
<tr>
<tr>
<td>Feb2011-Sales</td>
<td>10</td>
<td>10</td>
<td>10</td>
<tr>
etc.
我的问题是——如何迭代原始 HTML? 例如,我的想法是迭代第一个元素以获取行,然后获取父级的同级 div,以便我可以找到/匹配其子级以获取列。
想法? 谢谢。
I have the following simplified HTML Below.
<div id="coat">
<span class="Jan2011-Sales">10</span>
<span class="Feb2011-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="boot">
<span class="Jan2011-Sales">10</span>
<span class="Feb2011-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="hat">
<span class="Jan2011-Sales">10</span>
<span class="Feb-Sales">10</span>
<span class="Mar2011-Sales">10</span>
</div>
<div id="etc.">
</div>
EDITED:
What I'd like to do is create a table like below: (almost like a pivot table)
<th>
<td>Period</td>
<td>Coat</td>
<td>Boot</td>
<td>Hat</td>
</th>
<tr>
<td>Jan2011-Sales</td>
<td>10</td>
<td>10</td>
<td>10</td>
<tr>
<tr>
<td>Feb2011-Sales</td>
<td>10</td>
<td>10</td>
<td>10</td>
<tr>
etc.
My question is -- how do I iterate over the original HTML?
e.g. my thinking is along the lines of iterating over the first elements to get the rows, then getting the parent's sibling div so that I can find/match its child to get the columns.
Thoughts?
THank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个 jsFiddle,它获取 HTML 数据并从中创建表格: http://jsfiddle.net/jfriend00/ RKBAj/。
做出以下假设:
然后,您可以通过以下方式迭代数据并将所有数据收集到有组织的数据结构中,并从中构建表。
数据的存储方式如下:
而且,这是从数据创建表的一种方法。生成表的棘手部分是,如果任何产品可以有任意数量的月份,并且所有产品不一定具有相同的月份,那么您必须为存在的每个月份创建一行,并填写产品数据(如果有)该月的数据。
Here's a jsFiddle that takes your HTML data and make a table out of it: http://jsfiddle.net/jfriend00/RKBAj/.
Making these assumptions:
Then, here's how you could iterate over the data and collect all the data into an organized data structure from which you could build a table.
The data is stored like this:
And, this is one way you could create your table from the data. The tricky part of generating the table is that if any product can have any number of months and all products don't necessarily have the same months, then you have to make a row for every month that exists and fill in product data if it has data for that month.
http://jsfiddle.net/eqfEE/
你可能想知道为什么我使用数组而不是仅仅这样做字符串连接。原因很简单:数组的速度要快得多。
当连接字符串时,JavaScript 会丢弃旧字符串,并创建一个全新的字符串。这会导致创建和销毁过多的对象。
另一方面,当使用
.push()
时,旧数组永远不会被丢弃; JavaScript 只是在数组末尾添加一个新元素...现在您已经更新了您的问题并澄清了您希望将产品名称作为表标题,您应该执行以下操作:
使用此处的小提琴: http://jsfiddle.net/eqfEE/1/
注意:此代码可以改进。这只是朝着正确方向的一个推动。
http://jsfiddle.net/eqfEE/
You might wonder why I'm using an array instead of just doing string concatenation. The reason is very simple: array's are much faster.
When concatenating strings, JavaScript will discard the old string, and create a brand new one. This leads to too many objects being created and destroyed.
On the other hand, when using
.push()
, the old array is never discarded; JavaScript merely adds a new element to the end of the array...Now that you've updated your question and clarified that you want the product names as table headers, you should do the following:
With the fiddle here: http://jsfiddle.net/eqfEE/1/
Note: this code can be improved. This is just a nudge in the right direction.
对于完全不同的方式,您可以采用现有的 HTML,并通过简单地指定此 CSS 来显示每个产品的一列:
这将在每个产品列中从左到右布置产品 div,从上到下布置月份。
你可以在这里看到它: http://jsfiddle.net/jfriend00/V6Dnm/
显然,你可以使用更多 CSS 格式将其格式化为表格(分隔符、适当的间距)。并且,您可以使用 JS 在其上放置列或行标题。
For a completely different take, you can take the existing HTML and display it with a column for each product by simply specifying this CSS:
This will lay out the product divs left to right and the months top to bottom in each product column.
You can see it here: http://jsfiddle.net/jfriend00/V6Dnm/
Obviously, you could use more CSS formatting to format it as a table (dividers, appropriate spacing). And, you could use JS to put column or row headers on it.
为了进行选择,我最终使用了 2 个循环,如下所示:
To do the selections, I ended up using 2 loops, something like this: