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">«</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">»</span>
</a>
</li>
</ul>
</nav>
</div>
有一個問題,當你點擊下一個鏈接時,你是否也加載了一個新頁面?或者你說隱藏當前文本並顯示新文本? – Steven
只要您按照href的指示重新加載頁面。所以你的問題會是...? –
Steven:我希望每次下一次點擊時加載不同的html頁面。該代碼實際上是從一個索引加載列表的第一個HTML頁面,但當我點擊下一步它只是呆在那裏,不去第二頁。我認爲這與每次將實際頁面添加到實際頁面有關,但我不知道如何去做,嘗試使用這裏的一些代碼但不工作。謝謝。 –