简体   繁体   English

单击按钮时转到html页面中的下一个位置-jQuery

[英]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, 也就是说,

  • Previous Button -> clicking that it will go to previous div 上一个按钮->单击它将转到上一个div
  • Next Button -> clicking that will go to next div 下一步按钮->单击将转到下一个div

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.

相关问题 单击按钮时,转到下一页并刷新上一个Jquery Mobile - When clicking a button go to next page and refresh the previous one Jquery Mobile 单击“下一步”按钮时,jQuery选项卡突出显示 - Jquery Tabs Highlight When Clicking 'Next' Button 单击HTML中的输入时如何转到下一个选项卡? - How to go to the next tab when clicking on an input in HTML? 转到下一页-按钮 - Go to the next page - button jQuery:单击按钮时添加到html字符串 - jQuery: Add to html string when clicking on button 我需要创建一个HTML页面,其中隐藏了很少的文本字段,并且单击“下一步”按钮时需要使用javascript和jquery显示它们。 - I need to create an html page where few text fields are hidden and on clicking next button they need to be displayed using javascript and jquery? 单击旁边的按钮时获取ap标签的innerText(没有Jquery) - Get the innerText of a p tag when clicking on a button sitting next to it (no Jquery) 分页时有单独的按钮时,转到下一页? - Go to Next page when a separate button from pagination? 当我单击 gliderjs 中的下一个/上一个按钮时,页面 go 到顶部 - Page go to top when i click on next/prev button in gliderjs 如何通过单击下一步按钮使用Angular js转到具有不同数据的同一页面到laravel - how to go to same page with different data into laravel using angular js by clicking next button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM