Javascript 使用侧边栏菜单最有效的方法是什么?
所以,如果我必须制作一个侧栏菜单,比如学校网站,在页面加载后会突出显示活动的点击链接。它使用单独的页面,如Javascript 使用侧边栏菜单最有效的方法是什么?,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,所以,如果我必须制作一个侧栏菜单,比如学校网站,在页面加载后会突出显示活动的点击链接。它使用单独的页面,如 <a target="_top" href="default.asp">HTML HOME</a> <a target="_top" href="html_intro.asp">HTML Introduction</a> <a target="_top" href="html_editors.asp">HTML Editors&l
<a target="_top" href="default.asp">HTML HOME</a>
<a target="_top" href="html_intro.asp">HTML Introduction</a>
<a target="_top" href="html_editors.asp">HTML Editors</a>
<a target="_top" href="html_basic.asp">HTML Basic</a>
<a target="_top" href="html_elements.asp">HTML Elements</a>
但我自己构建的一个是用PHP创建的,所以其中发生的事情是菜单项的标题或名称被更改为小写,并保存在数据库中名为class的同一行中,该行用于活动链接高亮显示。所以,当点击侧边栏链接时,所发生的只是summary div内容的内容
(位于侧栏右侧)将更改,而侧栏和导航栏保持不变。所以它保持在同一页上,从数据库检索数据并放入summary div
<body class='noPadding'>
<div class='bigNav'> <!-- top navbar -->
<img src="images/logo2.png"/>
</div>
<?php
if(isset($_GET['subject'])){
$subject=$_GET['subject'];
?>
<div class='container'>
<div class='leftLayout' style='padding-top:70px;background-color:#EBEBEB'>
<ul class="fullFit">
<?php
$query="SELECT * FROM `study` WHERE `subject`='$subject'";
$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)){
$topic=$row['topic'];
$class=$row['class']; ?>
<li class="<?php echo $class?>">
<a href='<?php echo $sitename ?>/web.php?subject=<?php echo $subject?>&topic=<?php echo $class?>'>
<?php echo $topic?>
</a>
</li>
<?php } ?>
</ul>
</div>
<?php
if(isset($_GET['topic'])) {
$active_id=$_GET['topic'];
$topic=$_GET['topic'];
$query="SELECT summary FROM `study` WHERE `class`='$active_id'";
$run=mysqli_query($conn,$query);
$row=mysqli_fetch_array($run);
$summary=$row[0];
?>
<div class='summary'> <?php echo $summary ?> </div>
<?php } ?>
</div>
<script>
$(document).ready(function(){
$(".<?php echo $active_id ?>").addClass("current");
});
</script>
<?php } ?>
</body>
我希望这不会与您当前的结构有太大的偏离。
我不想把你引入兔子洞
尽管如此,听起来您可以将所有内容都编码到一个PHP文件中。请注意,这只是许多方法中的一种,高度依赖于项目的范围和单个编码样式
数据库
主题
`id`
`name`
主题
`id`
`subject_id`
`name`
`summary`
PHP
我推荐你的文件。例如,导航中的“HTML编辑器”可能链接到index.php?page=Editors
。在index.php
文件中,基于该page
变量包含一个editors.php
内容文件。这样,您就不需要在每个文件中重复主站点结构(标题、导航等)。您可能会发现这篇文章提供了信息:我所描述的内容被称为。根据项目的复杂性,为每个页面创建单独的文件(intro.php
,editors.php
,等等)并在每个文件中包含一个通用的navigation.php
,可能更简单或更有效。另请参阅本文中的示例:。谢谢您的评论。那么,我现在是否应该创建单独的文件并在每个文件中包含标题?您能否告诉我为什么不推荐我这样做?谢谢这是一个简单的例子。这个问题可能过于宽泛或基于观点,在这里无法有效回答。你能告诉我们更多关于你的网站内容吗?你把每个主要部分(简介、编辑、基础)都称为“主题”吗?每个主题是否包含多个主题?每个主题都有一个摘要?否决票是因为我不应该尝试回答如此广泛的问题吗?或者因为我没有回答关于如何组织文件的问题?还是别的什么?
<?php
// define database functions
function getAllSubjects($conn) {
$sql="SELECT * FROM `subject` WHERE 1;";
$q=$conn->query($sql);
return $q;
}
function getSubject($conn,$subject_id=false) {
$r=false;
if (is_numeric($subject_id)) {
$sql = "SELECT * FROM `subject` WHERE `id`=?;";
$q = $conn->prepare($sql);
$q->bind_param('i',$subject_id);
$q->execute();
$r=$q->fetch_assoc();
}
return $r;
}
function getSubjectTopics($conn,$subject_id=false) {
$q=false;
if (is_numeric($subject_id)) {
$sql = "SELECT * FROM `topic` WHERE `subject_id`=?;";
$q = $conn->prepare($sql);
$q->bind_param('i',$subject_id);
$q->execute();
}
return $q;
}
function getTopic($conn,$topic_id=false) {
$r=false;
if (is_numeric($topic_id)) {
$sql = "SELECT * FROM `topic` WHERE `id`=?;";
$q = $conn->prepare($sql);
$q->bind_param('i',$topic_id);
$q->execute();
$r=$q->fetch_assoc();
}
return $r;
}
// get URL parameters
$subject_id = !empty($_GET['s']) && is_numeric($_GET['s'])
? $_GET['s']
: false;
$topic_id = !empty($_GET['t']) && is_numeric($_GET['t'])
? $_GET['t']
: false;
// fetch all subjects from database
$subjectsQ=getAllSubjects($conn);
// fetch current subject
$thisSubject = $subject_id
? getSubject($conn,$subject_id)
: false;
// fetch current subject topics
$thisSubjectTopicsQ = !empty($thisSubject)
? getSubjectTopics($conn,$subject_id)
: false;
// fetch current topic
$thisTopic = $topic_id
? getTopic($conn,$topic_id)
: false;
// display subject navigation
while($subject = $subjectsQ->fetch_assoc()){
// determine if this subject is selected
$sel = !empty($thisSubject) && $thisSubject['id']==$subject['id']
? true
: false;
// define the URL
$url = '?s='.$subject['id'];
// display nav item for this subject
echo '<a href="' . $url . '"'
. ($sel?' class="sel"' : '')
. '>' . $subject['name']
. '</a>';
}
// if there are subject topics to display...
if ($thisSubjectTopicsQ && $thisSubjectTopicsQ->num_rows > 0) {
// loop through this subject's topics
while($topic=$thisSubjectTopicsQ->fetch_assoc()){
// determine if this topic is selected
$sel = !empty($thisTopic) && $thisTopic['id']==$topic['id']
? true
: false;
// define the URL
$url = '?s='.$thisSubject['id'].'&t='.$topic['id'];
// display nav item for this topic
// (alternate way to display the link)
?><a href="<?=$url?>"<?=$sel?' class="selected"':''?>><?php
echo $topic['name'];
?></a><?php
}
}
// if there is a topic to display
if (!empty($thisTopic)) {
// display topic summary
echo $thisTopic['summary'];
}
$sql="SELECT s.`id` AS `subject_id,
s.`name` AS `subject_name`,
t.`id` AS `topic_id,
t.`name` AS `topic_name`,
t.`summary` AS `topic_summary`
FROM `topic` t
LEFT JOIN `subject` s ON (s.`id`=t.`subject_id`)
WHERE t.`id`=?";