根据您的“元素”是什么,您可以简单地做(假设元素已经是一个 JQuery 对象):
http://api.jquery.com/position/
和
http://api.jquery.com/scrollTop/
$(window).scrollTop(element.position().top);
请记住,如果 element 已经是您不需要做的 JQuery 对象$(element)
。如果元素不是 jquery 对象,则它需要是 ID 或唯一选择器才能返回该元素。
编辑:动画支持
http://api.jquery.com/animate/
$(window).animate({"scrollTop": element.position().top});
编辑:地址从评论中选择一个元素
您的其他选择是为您的错误元素提供所有相同的类,这样您就可以始终(完成后)滚动到页面上的第一个错误。
var scrollPosition = Number($(".common-error-class:first").position.top);
然后从那里你只需要调用窗口来滚动:
$(window).scrollTop(scrollPosition);
编辑:经过验证的工作示例
//The line you are focusing on
$(window).scrollTop($("input:last").position().top);
完整的 html 文件:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="/Scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("#triggerScroll").on("click", function () {
$(window).scrollTop($("input:last").position().top);
});
});
</script>
</head>
<body>
<button id="triggerScroll">Scroll</button>
<div class="container">
<h2>Index</h2>
<form action="/Request/InvokeAction" method="post"> <input type="hidden" name="ActionID" value="10" />
<div class="editor-label"><label for="FirstName">FirstName</label></div>
<div class="editor-field"><input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="LastName">LastName</label></div>
<div class="editor-field"><input class="text-box single-line" id="LastName" name="LastName" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="Company">Company</label></div>
<div class="editor-field"><input class="text-box single-line" id="Company" name="Company" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Company" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="BirthMonth">BirthMonth</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-number="The field BirthMonth must be a number." data-val-required="The BirthMonth field is required." id="BirthMonth" name="BirthMonth" type="number" value="" /> <span class="field-validation-valid" data-valmsg-for="BirthMonth" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="BirthDay">BirthDay</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-number="The field BirthDay must be a number." data-val-required="The BirthDay field is required." id="BirthDay" name="BirthDay" type="number" value="" /> <span class="field-validation-valid" data-valmsg-for="BirthDay" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="BirthYear">BirthYear</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-number="The field BirthYear must be a number." data-val-required="The BirthYear field is required." id="BirthYear" name="BirthYear" type="number" value="" /> <span class="field-validation-valid" data-valmsg-for="BirthYear" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="BirthDate">BirthDate</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-date="The field BirthDate must be a date." data-val-required="The BirthDate field is required." id="BirthDate" name="BirthDate" type="datetime" value="" /> <span class="field-validation-valid" data-valmsg-for="BirthDate" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="Email">Email</label></div>
<div class="editor-field"><input class="text-box single-line" id="Email" name="Email" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span></div>
<div style="clear: both;"></div>
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>