的重載之後給單擊項目活動類我有幾個嵌套&隱藏子導航列表導航/ Subnav列表,如何
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li><a class="profile" href="#">Profile</a>
<ul id="profile">
<li><a href="company.html">Company</a></li>
<li><a href="structure.html">Structure</a></li>
<li><a href="team.html">Team</a></li>
</ul>
</li>
<li><a class="projects" href="#">Projects</a>
<ul id="projects">
<li><a href="chapter.html">Chapter</a></li>
<li><a href="pblc-trde.html">Pblc Trde</a></li>
<li><a href="globe.html">Globe</a></li>
<li><a href="komforte.html">Komforte</a></li>
</ul>
</li>
我目前使用的一些jQuery的我在網上找到點擊時顯示/隱藏子導航。我試圖完成的是:
希望清理sub-nab菜單的顯示/隱藏點擊功能。
當點擊子導航菜單項時,打開的相應頁面需要擴展子導航並給出相應的菜單項活動類,以便讓用戶知道它們在哪個頁面上。
我希望純粹在JS/jQuery中做到這一點。該網站的安裝將在WordPress。
$(document).ready(function() { $(".profile").click(function() { var X = $(this).attr('id'); if (X == 1) { $("#profile").hide(); $(this).attr('id', '0'); } else { $("#profile").show(); $(this).attr('id', '1'); } }); //Mouse click on nav $("#profile").mouseup(function() {}); //Document Click $(document).mouseup(function() { $("#profile").hide(); $(".profile").attr('id', ''); }); $(".projects").click(function() { var X = $(this).attr('id'); if (X == 1) { $("#projects").hide(); $(this).attr('id', '0'); } else { $("#projects").show(); $(this).attr('id', '1'); } }); //Mouse click on nav $("#projects").mouseup(function() {}); //Document Click $(document).mouseup(function() { $("#projects").hide(); $(".projects").attr('id', ''); }); }); window.onload = function() { $("ul#profile li:first").addClass("active"); }; $(document).ready(function() { $("ul#profile").show() });
您可以使用'document.location.href'(您需要對其進行一些操作)和'$('a [href = something]')'的組合來找到鏈接指向當前頁面。 – Dave 2013-03-18 23:36:14
我在回答中給代碼添加了評論,並做了一些調整以改進對多個子菜單級別的處理。 – Whistletoe 2013-03-19 02:04:32