めもむず

社畜を辞めてからのんびりWebのお仕事。Web話や就職や好きなこと語るめも。

URLの中身を読み取って違うclassをあてるjQuery

 

こんなとき

・URLによってデザインを切り分けしたい

・URLによって表示非表示したい(cssのdisplayと組み合わせ)

など。

※class割り当てだけでなくいろいろできます。

 

phpなどでもっとよい方法あるかとおもいますが

簡単なのでメモ。

 

このように記述

<script>
$(function(){
// URLにmatch部分が含まれていたら実行
if(document.URL.match("=brand")){
$('.tab_brand').addClass('select');
}
});
</script>

 

ifで分岐もできます。

<script>
$(function(){
// URLにmatch部分が含まれていたら実行
if(document.URL.match("=brand")){
$('.tab_brand').addClass('select');
} else
if (document.URL.match("=company")){
$('.tab_company').addClass('select');
} else
if (document.URL.match("=media")) {
$('.tab_media').addClass('select');
} else{
$('.tab_news').addClass('select');
}
});
</script>