【发布时间】:2017-08-16 01:11:03
【问题描述】:
我目前正在构建一个PHP 网页,该网页从SQL 服务器检索数据并将检索到的数据输出为表格。每个<tr> 都有自己的类,如下所示:
"<table class='preview'>
<tr class='title'>
<th>$pages->title</th>
</tr>
<tr class='info'>
<td class='type static'>Type: </td>
<td class='type dynamic'>$pages->type</td>
<td class='auteur static'>Written by: </td>
<td class='auteur dynamic'>$pages->author</td>
</tr>
<tr class='article'>
<td colspan='4'>$pages->article</td>
</tr>
<tr class='btns'>
<td colspan='4'>
<button type='button' class='more'>
See more
</button>
<button type='button' class='less'>
See less
</button>
</td>
</tr>
</table>"
由于有多篇文章而且它们很长,我想使用 display: none; 隐藏文章,直到用户点击“查看更多”按钮。
我用下面的 jQuery 代码做到了这一点:
var main = function() {
$('#search_container .preview').hover(function(){
$(this).toggleClass('active');
});
$('#search_container .active .more').click(function(){
console.log('click');
$('#search_container .active .less').show();
$('#search_container .active .article').show();
$('#search_container .active .more').hide();
$('#search_container .active .info').hide();
});
$("#search_container .active .less").click(function(){
$('#search_container .active .less').hide();
$('#search_container .active .article').hide();
$('#search_container .active .more').show();
$('#search_container .active .info').show();
});
};
$(document).ready(main);
由于只需要显示选定的文章,我决定添加第一个功能。但是.active .more 和.active .less 的选择器似乎不起作用,因为当我在没有.active 选择器的情况下执行完全相同的代码时,它可以工作并且所有文章和按钮都会显示或隐藏。
知道如何解决这个问题吗?
【问题讨论】:
-
您确定在悬停元素时添加了
active类吗? -
@RoryMcCrossan
.active是某个地方的标记。而OP只需要使用this或closest()。 -
$('#search_container').on('click', '.active .more', function() { -
@u_mulder 谢谢 - 我错过了
hover活动 -
除此之外,
$('#search_container .article')将匹配 所有<tr class='article'>元素。你需要在你的桌子上使用ids 或使用$(this)和parent或closest的一些技巧。
标签: javascript php jquery html mysqli