[英]Go to next position in html page when clicking a button - jquery
In my application I am using Jquery / Javascript I want to implement one functionality. 在我的应用程序中,我正在使用Jquery / Javascript,我想实现一种功能。
I have few divs like this, 我有几个这样的div,
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
I know that to go to specific position on that page, I can use, 我知道要转到该页面上的特定位置,我可以使用,
<a href="#Div4">go to div 4</a>
Now, I have two buttons already that are going to the previous div & going to next div. 现在,我已经有两个按钮转到上一个div和下一个div。
Ie, 也就是说,
How can I do this using this two buttons? 如何使用这两个按钮执行此操作?
Also, I only want to show the div where the button points. 另外,我只想显示按钮指向的div。 Ie, if clicking next button points to Div3 then only Div3 should be shown & other divs should be hidden. 即,如果单击下一步按钮指向Div3,则仅应显示Div3,而其他div应该被隐藏。
One way of doing this previous & next functionality is that when I introduce one variable which will be incremented or decremented when next or previous button clicked & depending on count of that variable I will decide which Div to point. 一种实现此上一个和下一个功能的方法是,当我引入一个变量时,单击下一个或上一个按钮时,该变量将递增或递减&取决于该变量的计数,我将决定指向哪个Div。 Ie, if variable value is 5 then I will point to Div5. 即,如果变量值为5,那么我将指向Div5。
Any other way any one know to do this? 还有其他人知道的其他方式吗?
You probably want to use the location.hash property and navigate around that using cached numbers, so assuming you have two buttons: "next" and "prev": 您可能想要使用location.hash属性并使用缓存的数字浏览该属性,因此假设您有两个按钮:“ next”和“ prev”:
var marker = 0;
$('#next').click(function(){
marker++;
location.hash = "div" + marker;
});
$('#prev').click(function(){
marker--;
location.hash = "div" + marker;
});
Obviously this needs expanded on, but you have the basic premise. 显然,这需要扩展,但是您有基本前提。
Jquery has next and prev selector to do this jQuery具有next和prev选择器来执行此操作
var currentDiv=$("#div1");
$("#btnNext").click(function()
{currentDiv=$(currentDiv);$(currentDiv).next().show();});
$("#btnPrev").click(function()currentDiv=$(currentDiv);
{$(currentDiv).prev().show();});
Note currentDiv should be a global variable. 注意currentDiv应该是一个全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.