在 jquery 中转到页面的某些部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8189109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Go to certain part of page in jquery?
提问by William Lewis
Is it possible to skip to a certain part of a page in jquery, for example I am on index.html and I want to go to the middle of the page on page2.html how do i make a link that does that? Thanks in advance for your answers. :)
是否可以在 jquery 中跳转到页面的某个部分,例如我在 index.html 上,我想转到 page2.html 上的页面中间,我该如何制作一个链接?预先感谢您的回答。:)
P.S. I have to use jquery because thats what im using for the whole site so the whole #location-on-page will NOT work :) Thanks
PS 我必须使用 jquery 因为这就是我在整个网站上使用的所以整个 #location-on-page 将不起作用 :) 谢谢
Snippet 1:
片段 1:
<a href="page.html#1"><img src="image_location/1.jpg" width="95%" border="1"></a>
Snippet 2:
片段 2:
<a href="image_folder/1.jpg"><img src="image_folder/1.jpg"><br>
Description
<a name="1"></a>
回答by Jeff Wilbert
If you need to go to a specific anchor via Javascript and not by clicking an anchor tag on the page then use
如果您需要通过 Javascript 转到特定锚点而不是通过单击页面上的锚点标签,则使用
location.href = "#anchor";
location.href = "#anchor";
jQuery isn't needed in this case but if you wanted too, you could simply create your own custom jQuery function
在这种情况下不需要 jQuery,但如果您也需要,您可以简单地创建自己的自定义 jQuery 函数
$.fn.gotoAnchor = function(anchor) {
location.href = this.selector;
}
$('#1').gotoAnchor();
// This way would also allow going to another pages anchors like so
$('page2.html#1').gotoAnchor();
or
或者
$.fn.gotoAnchor = function(anchor) {
location.href = '#' + this.selector;
}
$('1').gotoAnchor();
Depending on if you want to have to specifiy the anchor character #
in your selector.
取决于您是否想#
在选择器中指定锚字符。
回答by Daniel A. White
You have to put an anchor in. No need for jQuery!
你必须放一个锚点。不需要jQuery!
On page 2 - where you want it to jump to.
在第 2 页 - 您希望它跳转到的位置。
<a name="myanchor"></a>
On page 1.
在第 1 页。
<a href="page2.html#myanchor">Link</a>
回答by Billy Moon
That is a feature of HTML, not jQuery, so will work with both.
这是 HTML 的一个特性,而不是 jQuery,所以两者都可以使用。
On page2.html you need to include a named anchor tag where you want the page to go to...
在 page2.html 上,您需要在希望页面转到的位置包含一个命名的锚标记...
<a name="half-way"> </a>
Then in index.html, you can use an anchor tag that points there...
然后在 index.html 中,您可以使用指向那里的锚标记...
<a href="page2.html#half-way">This should get you half way..!</a>
You could use jQuery to generate these tags if you wanted to.
如果您愿意,您可以使用 jQuery 来生成这些标签。