如何使用文本输入定位? [英] How to use text-input to go to anchor?
本文介绍了如何使用文本输入定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jQuery通过文本框定位到锚点,例如,我想使用这种形式:
I want to use jQuery to go to an anchor with the use of a textbox, for example, I want to use this form:
<form id="gotoanchorform">
<input id="gotoanchorinput" type="text" />
<input type="submit" value="Go to anchor" />
</form>
在这种形式下,我将键入锚点的名称,当我单击转到锚点"按钮或按Enter键时,我希望页面向下滚动到该锚点:
In this form I would type the name of the anchor and when I click the "Go to anchor" button or press the enter key I want the page to scroll down to that anchor:
<a name="targetAnchor" id="targetAnchor">Target</a>
如何使它与jQuery一起使用?
How would get this to work with jQuery?
推荐答案
var anchor = $("#gotoanchorinput").val();
var position = $("#"+anchor).offset();
window.scrollTo(position.left, position.top);
这段代码使您的页面滚动到用#gotoanchorinput
编写的锚点.
This piece of code make your page scroll to the anchor written in #gotoanchorinput
.
这篇关于如何使用文本输入定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文