gpt4 book ai didi

jquery - 使用 Jquery 进行后退和下一步导航

转载 作者:行者123 更新时间:2023-12-01 05:19:34 26 4
gpt4 key购买 nike

我正在制作一个带有后退和前进链接的导航控件,以便您在链接列表中前进或后退。当您单击“下一步”时,它会前进到列表的第一页,但随后停留在那里。我如何将访问的链接设置为事件链接,以便它按顺序前进,即从 1 到 2,然后从 2 到 3 等...感谢您对此提供的任何帮助。

$(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>

Steven 在这个 Github 中提供了答案:

https://github.com/Steven0213/Back_and_next_nav

最佳答案

我创建了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'];

This array is the name array of your webpages. What does that mean by example you have a webpage called One, Two...

You place those names in the array.

If the names don't match it won't open any webpages and also not make the pages active because it don't find a match with the name of the current page.

编辑2

我创建了一个Github project创建一个演示。

关于jquery - 使用 Jquery 进行后退和下一步导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45892643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com