单击动态表的标题,淡出内容行
我对 jquery 和动态表有疑问。我想要单击表标题以使所有行(而不是标题)淡出。不幸的是我不知道如何选择行。到目前为止,我只找到了示例,其中它们具有固定的行数,因此具有固定的 ID 或类。但正如我所说,就我而言,可能有任意数量的行。
我想我需要类似 $(this).tr.effect(...) 的东西。 我知道我可以尝试用 Javascript 找出所有行的 ID,然后将它们淡出。但这似乎是解决该问题的过于复杂的方法。有人知道淡出行的简单方法吗?
编辑: 抱歉,我想我没有说清楚(我再试一次:))。我说:“我有一个动态表。”但我的意思是:我在一页上有动态表格,因此不止一个标题。像这样的东西(伪代码):
<table class="MyTable">
<header class="myHeader"/>
<rows />
</table>
<table class="MyTable">
<header class="myHeader"/>
<rows />
</table>
<table class="MyTable">
<header class="myHeader"/>
<rows/>
</table>
所有这些都是自动生成的。所以我不知道有多少个(当然我可以用javascript算出来或者把它写在隐藏字段中)。这就是为什么我无法使用 $("#myTable") 选择 ID 的原因。因为#myTable 不只一个。我的新方法是将我想要隐藏的行的 ID 编码到标头 ID 中,然后使用 $(".myHeader") 选择标头类。之后我可以解码 header-ID 并隐藏行。
我不确定这是否是一个明智的解决方案。那么也许有人有更好的? (我希望你们明白我想要做什么;))
我想问的是:
最聪明的代码是什么:
$(document).ready(function () {
$(".myHeader").click(function () {
//I try to hide the rows of the clicked header.
});
});
I have a problem with jquery and a dynamic table. I want a click on the table header to cause all the rows (not the header) to fade out. Unfortunately I can't figure out how to select the rows. Until now I've only found examples, where they have fixed number of rows and therefore fixed IDs or classes. But as I said, in my case there could be an arbitrary number of rows.
I guess I would need something like $(this).tr.effect(...).
I know that I could try to figure out all the ID's of the rows with Javascript and then fade them out. But this seems a too complex approach to that problem. Does someone know an easy way to fade out the rows?
Edit:
Sorry, I think I didn't make myself clear enough (I try again :)). I said: "I have a dynamic table." But what I meant was: I have dynamic tables on one page, and therefore more then one header. Something like this(pseudo code):
<table class="MyTable">
<header class="myHeader"/>
<rows />
</table>
<table class="MyTable">
<header class="myHeader"/>
<rows />
</table>
<table class="MyTable">
<header class="myHeader"/>
<rows/>
</table>
All this is auto generated. So I don't know how many there are (of course I could figure it out with javascript or write it in a hidden field). That's why I cant select an ID with $("#myTable") . Because there are more than one #myTable. My new approach is to code the ID of the rows I want to hide into the header ID and then select the header class with $(".myHeader"). After that I can decode the header-ID and hide the rows.
I am not sure, if this is a smart solution. So maybe someone has a better one?
(I hope you guys understand what I am trying to do ;))
What I am trying to ask is:
What is the smartest code to put in:
$(document).ready(function () {
$(".myHeader").click(function () {
//I try to hide the rows of the clicked header.
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的方法是将标题包装在
中,将行包装在
中,然后您可以使用以下代码:
但是如果您不想使用对于他们,此代码将起作用(假设只有第一行是标题行):
示例: http://jsfiddle.net/Wxa3W/1/
编辑:
更新问题的更新答案。
Html:
JavaScript:
如果您不想要 thead/tbody:
Html:
JavaScript:
示例: http://jsfiddle.net/Wxa3W/2/
The best way is to wrap your headers in
<thead>
and your rows in<tbody>
, then you can use this code:But if you dont want to use them, this code will work (assuming only the first row is your header row):
Examples: http://jsfiddle.net/Wxa3W/1/
Edit:
Updated answer to your updated question.
Html:
JavaScript:
And if you don't want thead/tbody:
Html:
JavaScript:
Example: http://jsfiddle.net/Wxa3W/2/
如果我误解了,请原谅,但是你就不能做这样的事情吗?
Please excuse me if i misunderstand, but can't you just do something like this?
您可以扭曲桌体。 (包裹你想要隐藏的内容。)
然后用jquery选择#table-body。
You can warp the table body. (Wrap what you want to hide.)
And then select the #table-body with jquery.
如果将标题行放在
元素中,并将表格正文放在
元素中,则可以使用以下内容jQuery达到了预期的效果。
If you place your header rows within a
<thead>
element, and the body of the table within a<tbody>
element, you would be able to use the following jQuery to achieve the intended effect.