使动态表格显示有限的结果并提供扩展选项
我有以下 HTML,我无法直接编辑它,因为它是动态生成的:
<table class="v65-productDisplay" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
</tbody>
</table>
TR 内的内容并不重要(我不认为),所以我将所有这些内容都保留了。 此代码块包含 1 个产品:
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
所以基本上我想使用 jQuery 来仅显示其中 4 个产品,并有一个按钮来展开/显示其余产品。我完全困惑了,不知道从哪里开始。我尝试找到每 4 个 TR,将它们包装在 div 中,然后尝试显示/隐藏它们,但它搞乱了我的布局,根本不起作用。如果有人可以提供帮助,将不胜感激!
编辑:现在使用这个:
<script type="text/javascript">
$(document).ready(function(){
var hiddenElements = $('.v65-productDisplay tr:gt(7)').hide();
if (hiddenElements.size() > 0) {
var showCaption = '<b>View </b>' + hiddenElements.size() + '<b> More Products</b>';
$('.v65-productDisplay').append(
$('<li id="toggler">' + showCaption + '</li>')
.toggle(
function() {
hiddenElements.show();
$(this).html('<b>Collapse</b>');
},
function() {
hiddenElements.hide();
$(this).text(showCaption);
}
)
);
}
});
</script>
工作完美,除了在“显示更多产品”的显示文本中它正在计算每个TR。因此,与其说“更多产品”,不如说“更多产品”。我如何编辑它以正确显示该文本?
I have the following HTML that I can't directly edit as it is being dynamically generated:
<table class="v65-productDisplay" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
</tbody>
</table>
It shouldn't matter what's inside the TR (I don't think) so I left all that stuff out.
This code block comprises 1 product:
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
So basically I would like to use jQuery to display only 4 of these products and have a button to expand/show the rest of them. I am thoroughly confused and don't know where to start. I tried finding each 4 of the TRs, wrapping them in a div, then trying to show/hide them but it screwed up my layout and didn't work at all. If anyone can help it would be much appreciated!
EDIT: Using this now:
<script type="text/javascript">
$(document).ready(function(){
var hiddenElements = $('.v65-productDisplay tr:gt(7)').hide();
if (hiddenElements.size() > 0) {
var showCaption = '<b>View </b>' + hiddenElements.size() + '<b> More Products</b>';
$('.v65-productDisplay').append(
$('<li id="toggler">' + showCaption + '</li>')
.toggle(
function() {
hiddenElements.show();
$(this).html('<b>Collapse</b>');
},
function() {
hiddenElements.hide();
$(this).text(showCaption);
}
)
);
}
});
</script>
Works perfectly except in the display text of "Show x more Products" it is counting every TR. So instead of saying "1 More Product" it is saying "4 More Products". How can I edit this to display that text correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忘记了
结束标签,我认为这会导致布局破坏。
那么如果您确实需要使用
我建议您首先放置 4
我喜欢使用的同一个类,例如
'
'
然后将新类添加到其他类,
在 css 文件中创建
添加
tr.toggle{display:none;}
然后在 js 中执行以下操作:
you forgot about
</tr>
ending tags i think that is causing layout broking.then if you really need to use a
<table>
i suggest you to put at first 4<tr>
a same class i like to use like'<tr class="show">'
then add new class to the others<tr class="toggle">
,create a
<a id="toggle"></a>
in css file add
tr.toggle{display:none;}
then in js do:
然后在文档就绪事件上调用 hideproducts。
Then call hideproducts on the document ready event.