こちらのサイトを参考に、現在URLの該当アコーディオンメニューを開いて
URL=hrefのaタグにactionのクラスを追加したいのですが
該当のアコーディオンは開けても、クラスの付与がどうもうまくいきません。
どうぞ宜しくお願い致します。
html
1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="UTF-8"> 5<link href="../css/menu.css" type="text/css" rel="stylesheet" /> 6<script src="../js/jquery.js"></script> 7<script src="../js/menu.js"></script> 8</head> 9<body> 10 11<div> 12<p>----- PAGE 11-----</p> 13</div> 14<p>----- SIDE MENU-----</p> 15 16<ul class="sidemenu"> 17<li> 18<div class="category">category1</div> 19<ul class="fxmn"> 20<li><a href="/menu/01/11.html#step01">menu 1-1</a></li> 21<li><a href="/menu/01/12.html">menu 1-2</a></li> 22<li><a href="/menu/01/13.html">menu 1-3</a></li> 23</ul> 24</li> 25<li> 26<div class="category">category2</div> 27<ul class="fxmn"> 28<li><a href="/menu/02/21.html">menu 2-1</a></li> 29<li><a href="/menu/02/22.html#step02">menu 2-2</a></li> 30<li><a href="/menu/02/23.html">menu 2-3</a></li> 31</ul> 32</li> 33<li> 34<div class="category">category3</div> 35<ul class="fxmn"> 36<li><a href="/menu/03/31.html">menu 3-1</a></li> 37<li><a href="/menu/03/32.html">menu 3-2</a></li> 38<li><a href="/menu/03/33.html#step03">menu 3-3</a></li> 39</ul> 40</li> 41</ul> </body> 42</html>
css
1.category { 2 line-height:35px; 3 cursor:pointer; 4 padding-left: 24px; 5 background-color:#ededed; 6 color:#333333; 7 border-top:solid 1px #ffffff; 8} 9 10ul.fxmn li a{ 11 height: 35px; 12 display:block; 13 color:#000; 14} 15 16ul.fxmn li{ 17 border-bottom:solid 1px #ededed; 18 line-height:35px; 19 padding-left: 31px; 20} 21.active { 22 color:#fff; 23 background-color:#000; 24}
js
1var currentPath = location.pathname.split("/"); 2var targetHref = "/" + currentPath[currentPath.length - 3] + "/" + currentPath[currentPath.length - 2] + "/" + currentPath[currentPath.length - 1]; 3targetHref = targetHref.split("index.html")[0]; //一応 4targetHref = targetHref + location.hash; 5document.write("<p>" + targetHref + "</p>"); 6 7navList = $(".sidemenu").find("a"); 8navList.each(function(){ 9 var myhref = $(this).attr("href"); 10 $document.write("<p>" + myhref + "</p>"); 11 if( myhref === targetHref ) { 12 $(this).addClass("active"); 13 } 14} ); 15 16$(function(){ 17$('ul.fxmn:not(:has(a[href="' + targetHref +'"]))').hide(); 18$("div.category").click(function(){ openMenu(this) }); 19}); 20function openMenu(category) { 21$("ul.fxmn").slideUp(); 22$(category).next().slideDown(); 23}
回答3件
あなたの回答
tips
プレビュー