[英]Set the li active based on the url
Can someone help me to find what's the problem to my code?有人可以帮我找出我的代码有什么问题吗? Im trying to set the li
to active based on it's url.我试图根据它的网址将li
设置为活动状态。 But the problem is nothing is set to active after loading.但问题是加载后没有任何设置为活动状态。 Give me ideas on how to do this or any alternative ways to do this?给我一些关于如何做到这一点或任何替代方法的想法?
NOTE the $subj_descr
had a space ex.注意$subj_descr
有一个空格 ex。 COMPUTER PROGRAMMING 1
, ENGLISH 2
COMPUTER PROGRAMMING 1
, ENGLISH 2
here's my php code where the li
and a
found.这是我找到li
和a
php 代码。
<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 id="wa" 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 id="<?php echo $subj_descr; ?>" ><a 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>
here's my js这是我的 js
$(document).ready(function() {
$(function(){
var current = location.pathname;
$('#wa li a').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
})
});
Can you try with following code:您可以尝试使用以下代码:
$(document).ready(function() {
$(function(){
//location.search will give us query string i.e. part present after ? mark and ? itself which is nothing but ?subjdescr=COMPUTER PROGRAMMING 1
var current = decodeURIComponent(location.search); //decodeURIComponent to decode white space chars like %20 will get converted to white space
alert(current);
$('#wa li a').each(function(){
var $this = $(this);
alert(decodeURIComponent($this.attr('href')));
// if the current path is like this link, make it active
if(decodeURIComponent($this.attr('href')).indexOf(current) !== -1){
$this.closest("li").addClass('active'); //setting the active class to parent li and not to a tag
}
})
})
});
I guess you need to set active
to li
tag and not to <a>
so just grab it via $.closest()
我想你需要将active
设置为li
标签而不是<a>
所以只需通过$.closest()
获取它
Note: added decodeURIComponent()
to avoid encoded chars in string comparision注意:添加了decodeURIComponent()
以避免字符串比较中的编码字符
Please try.请尝试。
Replace location.pathname
with window.location.href
.用window.location.href
替换location.pathname
。
$(document).ready(function() {
$(function(){
var current = window.location.href;
$('#wa li').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if(current.indexOf($this.find('a').attr('href')) !== -1){
$this.addClass('active');
}
})
})
});
You may use this to find the text in the URL您可以使用它来查找 URL 中的文本
var my_url = "localhost/studentportal/viewlecture.php?subjdescr=COMPUTER%20PROGRAMMING%202";
var found_my_url = my_url.search("viewlecture.php");
var found_hashed = my_url.search("\\?");
if(found_my_url!=-1 && found_hashed!=-1)
{
var url_split = my_url.split("?");
var url_final = url_split[1].split("=");
if(url_final[1]=="COMPUTER%20PROGRAMMING%202")
{
alert("Found in the URL");
}
else
{
alert("NOT FOUND in the URL");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.