我有側邊欄消失的iPad屏幕和更小的被替換下一個和以前的箭頭。jQuery - 小屏幕導航。 Next和prev按鈕href基於當前窗口url?
基本上,我想取當前頁面的URL,將它從域中分離出來,並將其與側欄中的定位標記進行比較。一旦找到正確的頁面,在側邊欄中的「當前頁面」標籤之前用錨標籤的href填充「上一個」圖標href,並用後面的錨標籤的href填充「下一個」圖標href。
sidebar.html(只是幾行,但真正的側邊欄很多更長)-
<nav class="main-nav">
<ul class="nav nav-pills nav-stacked">
<li>
<a href="{% url 'index:pg_1' %}" id="sub_1" ><h5>
{{title_1}}<span class=></span></h5></a>
</li>
<li>
<a href="{% url 'index:pg_2' %}" id="sub_1" ><h5>
{{title_2}}<span class=></span></h5></a>
</li>
<li>
<a href="{% url 'index:pg_3' %}" id="sub_1"><h5>
{{title_3}}<span class=></span></h5></a>
</li>
<li>
<a href="{% url 'index:pg_4' %}" id="sub_1"><h5>
{{title_4}}<span class=></span></h5></a>
</li>
圖標 -
<i class="fa fa-angle-left fa-5x prev" aria-hidden="true">
<a class="left" href="#"></a>
</i>
<i class="fa fa-angle-right fa-5x next" aria-hidden="true">
<a href="#" class="right"></a>
</i>
的jQuery -
$(function(){
console.log("nav for sm screens is ready");
// var url;
var x;
var url;
var page_url = window.location.pathname;
page_url.split('/');
console.log(page_url);
url = $("a#sub_1").attr("href"); // Gets all 'a' tags with id of 'sub_1' (all of them)
console.log("we have urls ");
for (x in url){ // loops through them
if (page_url == url){ // matches page url with a url in sidebar
console.log("we have a url that is the same as the page url")
console.log(url)
if ($(this).closest("li a").prev().length) { // code usually breaks down here, checking if an anchor tag exists before current selection or not
console.log("there are no links before this")
$(".prev").hide(); // if no anchor tag exists before this tag, hide previous.
console.log("left is hidden");
$(".right").prop("href", $(this).next("a").prop("href")); // set 'next' to href of next anchor tag.
console.log("we have next link");
};
};
};
});
我試過使用closest()
,parent()
,parents()
和一些其他變化,但似乎沒有任何工作。
你在控制檯中看到了什麼?你不需要'var parts = page_url.split('/');'來捕獲結果數組? – Twisty
'id'屬性應該是唯一的。因此,對所有人使用'sub_1'會導致問題。會建議使用'class':'',and'',然後使用'$(」。sub-link「)'選擇器。 – Twisty
@Twisty在控制檯中,它不會越過檢查當前錨點之前是否存在錨點的線路。它一直運行一次或兩次,但無論如何都不起作用。 – arm93