私がネットから降りたこのコードについて助けが必要です
$(document).ready(function(){
jQuery(".linkbar li").each(function(){
var href = jQuery(this).find('a').attr('href');
if (href === window.location.pathname) {
$(this).addClass('current');
}
});
});
document.write(href);を実行した場合
「未定義」になります
href = jQuery(this).find('a').attr('href');
関数の前にvarを配置.each()
すると、すべてのリンクが現在のクラスに変更されます
以下は私のHTMLとCSSです。現在のページのcssリンクを変更する必要があります。Jquery/JSはあまり得意ではありません。問題とその解決方法を教えてください。
<div class ="linkbar">
<a href = "/HTS/about-us.php"><li> ABOUT US </li> </a>
<a href = "service.php"><li> SERVICES </li> </a>
<a href = "download.php"><li> DOWNLOAD </li> </a>
</div>
CSS
.linkbar li{
text-decoration:none;
float:left;
list-style-type: none;
font-size:11px;
width:auto;
padding:9px 18px 9px 18px;
}
.current{
background-color:#fecd0f;
vertical-align:center;
color:#fff;
}