我想使用javascript让我的li
处于活动状态。但问题是,只需点击一下就无法正常工作。在激活之前需要连续2次点击。有人可以帮我这个吗?
<li data-popover="true" rel="popover" data-placement="right"><a href="#" data-target=".premium-menu" class="nav-header collapsed" data-toggle="collapse"><i class="fa fa-fw fa-book"></i> Lectures<i class="fa fa-collapse"></i></a></li>
<li><ul class="premium-menu nav nav-list collapse in">
<?php
$sql ="SELECT enroll_ref FROM std_enrolled WHERE stud_no = '$stud_no'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$enroll_ref = $row['enroll_ref'];
}
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$enroll_ref'";
$results = mysqli_query($con, $sql3);
while($row = mysqli_fetch_array($results)){
$subj_descr = $row['subj_descr'];
?>
<li><a class="item" href="viewlecture.php?subjdescr=<?php echo $subj_descr;?>"><span class="fa fa-caret-right"></span><?php echo ucwords(strtolower($subj_descr)); ?></a></li>
<?php
}
?>
</ul></li>
<script type="text/javascript">
$(".item").click(function() {
$(this).parent('li').addClass("active");
});
</script>
答案 0 :(得分:0)
Ajax页面
$(".item").on("click",function(e) {
e.preventDefault(); // stop link from reloading the page
$("#lectureContainer").load(this.href); // load the lecture into a div or so
$(this).closest("ul").find("li").removeClass("active");
$(this).parent('li').addClass("active");
});
有关更多示例,请查看Set active link based on URL