我想使用 jQuery 通过文本框转到 anchor ,例如,我想使用以下形式:
<form id="gotoanchorform">
<input id="gotoanchorinput" type="text" />
<input type="submit" value="Go to anchor" />
</form>
在此表单中,我将键入 anchor 的名称,当我单击“转到 anchor ”按钮或按 Enter 键时,我希望页面向下滚动到该 anchor :
<a name="targetAnchor" id="targetAnchor">Target</a>
如何让它与 jQuery 一起工作?
最佳答案
var anchor = $("#gotoanchorinput").val();
var position = $("#"+anchor).offset();
window.scrollTo(position.left, position.top);
这段代码使您的页面滚动到用#gotoanchorinput
编写的 anchor 。
关于jquery - 如何使用文本输入来定位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12250396/