简体   繁体   English

使用Javascript创建“转到页面”按钮

[英]Create “go to page” button using Javascript

I am new to javascript, but have made an html page with 46 images on it which scroll horizontally. 我是javascript的新手,但是制作了一个html页面,上面有46张图像,可水平滚动。 Each image is a different page of a magazine. 每个图像都是杂志的不同页面。 Each image has an id of 1 through 46. 每个图像的ID为1到46。

How can I use javascript to make this button be a link to the divs 1 through 46 and take the user to that # link when they press "Go"? 如何使用javascript使此按钮成为指向div 1至46的链接,并在用户按“ Go”时将其带到该#链接?

<form id="goto" name="gotoform">
    Go to Page: <input type="number" name="gotopage" min="1" max="46">
    <input type="submit" value="Go">
</form>

For example, if the user is on example.com/index.html and enters 6 and presses go, they will be taken to example.com/index.html#6 例如,如果用户在example.com/index.html上并输入6并按go,则他们将被带到example.com/index.html#6

Thanks 谢谢

You can navigate to any id with the help of the anchor tag. 您可以在锚标记的帮助下导航到任何ID。

<input type="submit" value="Go" onclick="return jump();">

and then: 接着:

function jump(){
    location.href = "#"+<any prefix for the div ids>+document.getElementsByName("gotopage")[0].value;
}   

There's a jquery plugin just to do that in a fancy way - ScrollTo: http://demos.flesler.com/jquery/scrollTo/ . 有一个jquery插件只是以一种奇特的方式做到这一点-ScrollTo: http : //demos.flesler.com/jquery/scrollTo/ It adds a nice little animation. 它添加了一个不错的小动画。

I suggest you have a good look at this page to find out how to ask a question here: 我建议您对此页面有个很好的了解,以了解如何在此处提出问题:

https://stackoverflow.com/help/how-to-ask https://stackoverflow.com/help/how-to-ask

But to answer your question, something like this should suffice: 但是要回答您的问题,这样的话就足够了:

$(function() {
    $("form#goto input[type='submit']").click(function(e) {
        window.location = "#" + $("input[name=gotopage]").val();
        e.preventDefault();
    });
});

I would just use some jQuery to attach an event to your GO button... like so 我只是使用一些jQuery将事件附加到您的GO按钮...

$('input[type="submit"]').click(function () {
  var page = $('input[type="number"]').val();
  location.href = "index.html#" + page;
});

This captures the value in your input field, and then appends it to the URL specified, in addition to taking you to that location. 这将捕获您输入字段中的值,然后将其附加到指定的URL(除了将您带到该位置之外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM