gpt4 book ai didi

asp.net - 服务器端的 .Focus() 不会滚动到 View 中

转载 作者:行者123 更新时间:2023-11-30 18:54:15 24 4
gpt4 key购买 nike

自定义验证控件:

<asp:CustomValidator
ID="valSex" runat="server" ErrorMessage="1.2 &lt;b&gt;Sex&lt;/b&gt; not specified"
EnableClientScript="true" ClientValidationFunction="ValidateSex" SetFocusOnError="True">Selection required</asp:CustomValidator>

客户端验证例程:

function ValidateSex(source, args) {

var optMale = document.getElementById("<%=optMale.ClientID %>");
var optFemale = document.getElementById("<%=optFemale.ClientID %>");

if (!optMale.checked && !optFemale.checked) {
args.IsValid = false;
optMale.focus();
}
else
args.IsValid = true;
}

当提交页面且未指定 Sex 时,焦点已设置但 2 个单选按钮不在 View 中,需要垂直滚动才能将它们显示在 View 中。

Focus() 方法不应该将焦点控件置于 View 中吗?

最佳答案

我不认为所有浏览器都必须以这种方式实现它。您可以使用 scrollTo(x, y) 将其滚动到 View 中。

以下应该可以解决问题:

function scrollToElement(elementId)
var offset = findPos(document.getElementById(elementId));
var x = offset[0];
var y = offset[1];

window.scrollTo(x, y);
}

function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);// yes, it's an assignment
}
return [curleft,curtop];
}

更多关于寻找职位的信息 http://www.quirksmode.org/js/findpos.html .

关于asp.net - 服务器端的 <Control>.Focus() 不会滚动到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2751075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com