2017-08-26 118 views
2

使用後退和前進鏈接進行導航控制,以便前進或返回鏈接列表。當你點擊下一步時,它會前進到列表的第一頁,但隨後停留在那裏。 如何將訪問鏈接設置爲活動狀態,以便順序進行,即從1到2,然後從2到3等。 感謝您的幫助。上一頁和下一頁使用Jquery導航

$(document).ready(function() { 
 
      $('.next-button').click(function() { 
 
       var $el = $('#nav li a.navSelected').removeClass('navSelected'); 
 
       var $next = $el.parent().next(); 
 
       if ($next.length == 0) $next = $('#nav li:first'); 
 
       $next.find('a.nav-button').addClass('navSelected'); 
 
       // Added window.location.href to follow the selected links href 
 
       window.location.href = $next.find('a.nav-button').attr('href'); 
 
      }); 
 
      $('.prev-button').click(function() { 
 
       var $el = $('#nav li a.navSelected').removeClass('navSelected'); 
 
       var $prev = $el.parent().prev(); 
 
       if ($prev.length == 0) $prev = $('#nav li:last'); 
 
       $prev.find('a.nav-button').addClass('navSelected'); 
 
       // Added window.location.href to follow the selected links href 
 
       window.location.href = $prev.find('a.nav-button').attr('href'); 
 
      }); 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul id="nav"> 
 
<li><a href="pagea.html"class="nav-button">A</a></li> 
 
<li><a href="pageb.html" class="nav-button">B</a></li> 
 
<li><a href="pagec.html" class="nav-button">C</a></li> 
 
<li><a href="paged.html"class="nav-button">D</a></li> 
 
<li><a href="pagef.html" class="nav-button">F</a></li> 
 
</ul> 
 
<div id="arrow-left" class="slide-right"><a href="#" class="prev-button" 
 
title="Previous"><img src="images/arrow-left.png" alt="Left Arrow" width="25" 
 
height="48"></a></div> 
 
<div id="arrow-right" class="slide-left"><a href="#" class="next-button" 
 
title="Next"><img src="images/arrow-right.png" alt="Right Arrow" width="25" 
 
height="48"></a></div>

答案已在此Github上提供了由史蒂芬:

https://github.com/Steven0213/Back_and_next_nav

+0

有一個問題,當你點擊下一個鏈接時,你是否也加載了一個新頁面?或者你說隱藏當前文本並顯示新文本? – Steven

+0

只要您按照href的指示重新加載頁面。所以你的問題會是...? –

+0

Steven:我希望每次下一次點擊時加載不同的html頁面。該代碼實際上是從一個索引加載列表的第一個HTML頁面,但當我點擊下一步它只是呆在那裏,不去第二頁。我認爲這與每次將實際頁面添加到實際頁面有關,但我不知道如何去做,嘗試使用這裏的一些代碼但不工作。謝謝。 –

回答

0

我創建this搗鼓你。 它與Bootstrap和JQuery。 我不知道如果你知道Bootstrap和JQuery。 如果您還有其他問題,請詢問。

1

var ArrayOfPages = ['One', 'Two', 'Three', 'Four', 'Five']; 
 

 

 
var makeTheRightOneActiveWhenThePageStart = function() { 
 
    var url = window.location.href; 
 
    for (var i = 0; i < ArrayOfPages.length; i++){ 
 
     var newUrl = ArrayOfPages[i] + '.html'; 
 
     if (url.indexOf(newUrl) > 1){ 
 
      var newI = i + 1; 
 
      $("a:contains("+ newI + ")").parent().addClass('active'); 
 
     } } }; 
 

 
var makeTheRightOneActive = function() { var $selector = $(this); var getClass = $selector.attr('class'); var getHTML = $selector.html(); var whatTheSetTrue; switch (getClass){ 
 
     case 'prev': 
 
      whatTheSetTrue = getTheRightOneActiveWhenPressPrevOrNext('prev',getHTML); 
 
      break; 
 
     case 'next': 
 
      whatTheSetTrue = getTheRightOneActiveWhenPressPrevOrNext('next',getHTML); 
 
      break; 
 
     default: 
 
      whatTheSetTrue = getTheRightOneActiveWhenPressPrevOrNext('',getHTML); 
 
      break; } window.open(whatTheSetTrue + '.html', '_self', false) }; 
 

 
var getTheRightOneActiveWhenPressPrevOrNext = function (whatMethode,whichNumber) { 
 
    var whatPageAreYouOn = $('.active').children().html(); 
 
    var whatCurrentPlaceAreYouOn = findAtWhichPlaceYouAreInTheArray(whatPageAreYouOn); 
 
    var newPlace; 
 
    switch (whatMethode){ 
 
     case 'next': 
 
      newPlace = whatCurrentPlaceAreYouOn + 1; 
 
      if (newPlace === 5){ 
 
       newPlace = 4; 
 
      } 
 
      break; 
 
     case 'prev': 
 
      newPlace = whatCurrentPlaceAreYouOn - 1; 
 
      if (newPlace === -1){ 
 
       newPlace = 0; 
 
      } 
 
      break; 
 
     default: 
 
      newPlace = findAtWhichPlaceYouAreInTheArray(whichNumber); 
 
      break; 
 
    } 
 
    return ArrayOfPages[newPlace] }; 
 

 
var findAtWhichPlaceYouAreInTheArray = function (whatPageAreYouOn) { 
 
    whatPageAreYouOn = parseInt(whatPageAreYouOn); 
 
    return whatPageAreYouOn - 1; }; 
 

 
var init = function() { 
 
    makeTheRightOneActiveWhenThePageStart(); 
 
    $('.container .pagination a').on('click',makeTheRightOneActive); }; $(document).ready(init());
div.pageOne, div.pageTwo, div.pageThree, div.pageFour, div.pageFive{ 
 
    margin-top: 15px; 
 
} 
 

 
#nav{ 
 
    text-align: center; 
 
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 

 
<div class="container"> 
 
    <div class="pageOne"> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas imperdiet, risus a rutrum eleifend, nulla lorem laoreet dolor, ut vehicula quam magna et est. Pellentesque egestas sem et ligula tincidunt finibus. Nullam a efficitur mi, eget rutrum 
 
     dolor. Maecenas aliquet nibh quis vehicula gravida. Ut in finibus magna. Sed sagittis, velit ac cursus auctor, tortor dui mattis metus, sed commodo urna erat congue justo. Morbi porta nisi mattis lobortis aliquam. Phasellus nec nibh in ipsum scelerisque 
 
     dignissim. Morbi id diam ut purus rhoncus suscipit placerat at libero. Curabitur quis eros maximus, pharetra urna eget, facilisis ex. Etiam commodo orci aliquet odio fermentum pulvinar. Duis auctor pellentesque condimentum. Duis ut nisi sem. Nulla 
 
     in augue sed augue euismod feugiat in in nunc. Aliquam vel enim eros. Pellentesque leo dui, iaculis quis congue eu, ultrices id nisl.</p> 
 
    </div> 
 
    <div class="pageTwo"> 
 
    <p>Cras vehicula ipsum ac eros porta dapibus. In eu ante varius, porta lectus sit amet, sagittis ipsum. Mauris congue velit mauris, condimentum sollicitudin mi luctus quis. Aliquam erat volutpat. Praesent justo erat, facilisis id eleifend non, accumsan 
 
     vel arcu. Sed id dui malesuada, vulputate velit sit amet, porta orci. Nam eu nunc eu erat accumsan ornare. Vivamus dui lacus, pharetra a bibendum nec, molestie non justo. Praesent at quam urna. Integer pharetra suscipit arcu, in sodales turpis mollis 
 
     ut. Etiam placerat eget ante in congue.</p> 
 
    </div> 
 
    <div class="pageThree"> 
 
    <p>Donec pretium ex a dolor luctus, eu malesuada metus condimentum. Aliquam consequat vestibulum massa, id iaculis magna aliquet malesuada. Nunc interdum turpis vitae metus feugiat, fringilla faucibus libero egestas. Donec tincidunt luctus gravida. Maecenas 
 
     tincidunt interdum enim, vel consequat lacus auctor eu. Vestibulum metus lorem, posuere at gravida nec, lobortis vitae sapien. In auctor tempus pretium. Aliquam dictum urna ac aliquet imperdiet. Ut egestas eros et ante ornare, et pretium erat scelerisque. 
 
     Donec libero nunc, posuere vitae massa non, tempus lacinia nisi.</p> 
 
    </div> 
 
    <div class="pageFour"> 
 
    <p>Nunc a nisl vitae tortor consequat ultrices. Suspendisse tincidunt rutrum ipsum in dictum. Proin tempus mi quis sollicitudin aliquam. Maecenas scelerisque id lectus et fermentum. Morbi dictum elit eu nisi convallis sagittis. In hac habitasse platea 
 
     dictumst. In neque lectus, suscipit non quam sed, viverra viverra nisl. Donec at ex non velit vestibulum molestie a eget ex. In faucibus viverra purus, nec dapibus diam feugiat eu. Class aptent taciti sociosqu ad litora torquent per conubia nostra, 
 
     per inceptos himenaeos.</p> 
 
    </div> 
 
    <div class="pageFive"> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In et commodo sapien. Vestibulum rutrum nec nibh ac aliquet. Vestibulum lacus felis, vulputate et pretium eu, posuere euismod sapien. Sed varius vel sapien non dapibus. Nullam metus lectus, 
 
     fringilla quis orci ac, iaculis consectetur ipsum. Curabitur magna purus, interdum non accumsan quis, bibendum euismod ante. Curabitur non suscipit ligula. Aliquam id leo ac dolor eleifend mattis. Maecenas neque dui, vestibulum vel massa nec, pharetra 
 
     sagittis libero. Nunc sit amet aliquet arcu, tempor dapibus lectus. Suspendisse laoreet massa ac tortor egestas sollicitudin. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p> 
 
    </div> 
 
    <nav id="nav" aria-label="Page navigation"> 
 
    <ul class="pagination"> 
 
     <li> 
 
     <a href="#" aria-label="Previous" class="prev"> 
 
      <span aria-hidden="true">&laquo;</span> 
 
     </a> 
 
     </li> 
 
     <li class="active"><a href="#" class="One">1</a></li> 
 
     <li><a href="#" class="Two">2</a></li> 
 
     <li><a href="#" class="Three">3</a></li> 
 
     <li><a href="#" class="Four">4</a></li> 
 
     <li><a href="#" class="Five">5</a></li> 
 
     <li> 
 
     <a href="#" aria-label="Next" class="next"> 
 
      <span aria-hidden="true">&raquo;</span> 
 
     </a> 
 
     </li> 
 
    </ul> 
 
    </nav> 
 
</div>

編輯1

var ArrayOfPages = ['One', 'Two', 'Three', 'Four', 'Five']; 

這個數組是你的網頁的名稱數組。這是什麼意思,例如你有一個網頁稱爲一,二...

你把這些名字放在數組中。

如果名稱不匹配,則不會打開任何網頁,也不會使頁面處於活動狀態,因爲它找不到當前頁面的名稱與 的名稱匹配。

EDIT 2

我創建了一個Github project創建演示。

+0

非常感謝您的幫助。你的代碼工作正常,但它從同一頁面中的一系列DIVS加載DIV的權利?我需要從一個頁面導航到另一個頁面,它們是不同的.html文件。所以說,列表中的不同鏈接。與DIVS的事情是,我不能做一個巨大的單頁與很多不同的DIVS即時通訊尋找一個簡單的菜單,加載不同的HTML頁面。 –

+0

是的,因爲我教這是最好的選擇。那麼你的html頁面也少了。 – Steven

+0

是的,問題在於,這是一個非常大的網站,超過一千頁,當我在HTML文件上放置超過3頁的文件時,它會達到5 MB左右,這太多了,使網站真的很慢。我現在的代碼用於從列表中加載第一頁(url),但它只是停留在那裏。我認爲這與每次將實際頁面添加到實際頁面有關,但我不知道如何去做,嘗試使用這裏的一些代碼但不工作。謝謝。 –

相關問題