点击时jQuery展开列表不展开

时间:2014-07-27 17:53:26

标签: javascript php jquery html

我被困在这个剧本上。基本上我有不明数量的类别(20-50),它们显示在一个侧边栏上,一个在另一个上面。我想要显示10,然后当我按下按钮时,它将展开以显示其余部分。我有10个暴露,其余的数量在一个名为cathide的div中关闭。因此,当我按下按钮显示时,它应该展开但不是。

PHP代码生成列表,显示10个类别,然后回显

<div class="cathide">

然后它显示剩余的数量,然后是回声

</div>

这就是将重新定义包含在div中的方式。

以下是代码..

PHP

if ( $all_categories ) {
$count = 1;

foreach ( $all_categories as $cid => $arr ) {
    $sidebar .= '<a href="index.php?action=sort&cid=' . $cid . '">' . $arr['name'] . ' (' . $arr['count'] . ')</a><br />';

    if ( 10 == $count ) {
        $sidebar .= '<div class="cathide">';
    }

    $count++;
}
$sidebar1 .= '</div>';

} else {
$sidebar = 'There are no categories yet';
}

CSS

.cathide {
    display:none;
}

的jQuery

$("#showAll").click(function () {
$("cathide").slideDown(500);
$(this).hide();
});

$("#hide").click(function () {
$("cathide").slideUp(500, function () {
    $("#showAll").show();
});

});

HTML

<table border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder">
<tr>
<td class="tab">
Categories
</td>
</tr>
<tr>
<td class="trow2">

{$sidebar} // Used to call 10 cats then div class=cathide then remaining cats

{$sidebar1} // Used to call /div
<input type="button" value="Show all" id="showAll" />
<input type="button" value="Hide" id="hide" />

</td>
</tr>
</table>

1 个答案:

答案 0 :(得分:0)

如上所述,您错过了window.onload侦听器,因此您需要执行此操作:

$(window).ready( function() { 

    $("#showAll").click(function () {
       $(".cathide").slideDown(500);
       $(this).hide();
    });

   $("#hide").click(function () {
       $(".cathide").slideUp(500, function () {
    $("#showAll").show();
    });

});
相关问题