2015-10-18 46 views
0

點擊作出積極和加載頁面的鏈接被指向

$(function() { 
 
\t $('a:first-child').addClass('active'); 
 
    $('a').click(function() { 
 
\t $('.active').removeClass('active'); 
 
    $(this).addClass('active'); 
 
    }); 
 
});
nav a { 
 
\t color:#333; 
 
\t background:#186467; 
 
\t padding:10px; 
 
} 
 
.active { 
 
\t color:#fff; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<nav> 
 
<a href="index.html">HOME</a> 
 
<a href="about.html">ABOUT</a> 
 
<a href="contact.html">CONTACT</a> 
 
</nav>

傢伙當我點擊一個指向我,我想要的鏈接,而是主動犯規work.Active「HOME」仍然存在。

如果我添加preventDefault()頁面不指向鏈接。

+0

這似乎很好地工作 –

+0

耶我不明白爲什麼以及在哪裏丟失.. – Norx

+0

什麼是不爲你工作?我看到,當我們點擊鏈接時,它會變成白色並重定向到頁面。 –

回答

0

對於那些感興趣的人,我找到答案;

var path = window.location.href; 
$('a').each(function() { 
    if (this.href === path) { 
     $(this).addClass('active'); 
    } 
}); 
相關問題