我有一个PHP脚本,它从2个独立的SQL表生成ul / li表。但是当我尝试使用js使它们可折叠时,它只是不起作用。
我做错了吗?或者我错过了什么?任何帮助深表感谢。
JScript:
<script type="text/javascript" language="javascript">
$('#usernav').find('ul').slideUp();
$('.child').hide();
$('.parent').click(function() {
$('#usernav').find('ul').slideUp();
$(this).find('ul').slideToggle();
});
</script>
PHP:
<?php require_once('/Connections/TDBS_local.php');
mysql_select_db($database_TDBS_local, $TDBS_local);
$main_cat="SELECT * FROM main_category";
$main_query = mysql_query($main_cat, $TDBS_local) or die(mysql_error());
$main_result = mysql_fetch_assoc($main_query);
echo("<ul id='usernav'>");
do {
echo("<li class='parent'>");
$main_link = preg_replace("/[^A-Za-z0-9 ]/", '', $main_result['main_category']);
$main_link = strtolower($main_link);
$main_link = str_replace (' ','',$main_link);
echo "<a href='categories/".$main_link."/index.php'>" .($main_result['main_category']). "</a>";
echo("<ul class='child'>");
$main_id = ($main_result['id']);
$sub_cat="SELECT * FROM sub_category WHERE main_category_id = $main_id ";
$sub_query = mysql_query($sub_cat, $TDBS_local) or die(mysql_error());
$sub_result = mysql_fetch_assoc($sub_query);
do {
$sub_link = preg_replace("/[^A-Za-z0-9 ]/", '', $sub_result['sub_category']);
$sub_link = strtolower($sub_link);
$sub_link = str_replace (' ','',$sub_link);
echo("<li><a href='categories/".$main_link."/".$sub_link.".php'>" .$sub_result['sub_category']. "</a></li>");
} while ($sub_result = mysql_fetch_assoc($sub_query));
echo(" </ul>
</li>");
} while ($main_result = mysql_fetch_assoc($main_query));
echo("</ul>");
mysql_free_result($main_query);
mysql_close($TDBS_local);
?>
答案 0 :(得分:1)
试试这个..
<script>
$(function(){
$('#usernav').find('ul').slideUp();
$('.child').hide();
$('.parent').click(function() {
$('#usernav').find('ul').slideUp();
$(this).find('ul').slideToggle();
});
});
</sciprt>