当我的页面首次加载时,我试图将注意力集中在页面上的第一个文本框或输入框上。所以我希望在页面加载时在第一个文本框中显示一个闪烁的光标。我在谷歌上搜索了许多解决方案,所有结果都相同:使用 .focus() 协议(protocol)。我已经尝试过但仍然不起作用。
这是在我的初始化函数中:
$(document).ready(function () {
$('#TranslationTextBox:first').focus();
$(".parametersNumericbox:first").focus();
});
$(".parametersNumericbox").focus(function () {
var input = $(this);
setTimeout(function () {
input.select();
});
});
只有当我多次点击 Tab 时,它才会转到第一个文本框。
编辑:html代码(使用Kendo UI):
<td id="TranslationTextBox">
@(Html.Kendo().NumericTextBoxFor<double>(model => model.Translation).Format("#").Decimals(0).Min(0)
.HtmlAttributes(new { @class = "parametersNumericbox" })
.Events(e => e
.Change("TranslationTextBoxChanged")
))
</td>
我当前遇到的问题是,由于某种原因,焦点似乎转到了页面上文本字段之前的按钮。
最佳答案
当然,您可以使用 Javascript,但您也可以使用 HTML:
<input type="text" autofocus>
在 jQuery 中你可以这样做:
$("input, textarea, select").filter(":first").focus();
您的代码的问题似乎是您正在关注多个项目。当您这样做时,最后聚焦的项目将获得焦点。
关于javascript - 页面加载时如何聚焦于输入框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27895544/