简体   繁体   English

document.getElementById()。focus()返回undefined

[英]document.getElementById().focus() returning undefined

I have the following HTML: 我有以下HTML:

<input type="text" id="endDateEditBox" value="" style="margin-left:5px; height:18px; width:70px; vertical-align:middle;" onchange="validateDate('endDateEditBox', $(dateFormatErrorString).value)">

And I am trying to make this focus using the following Javascript: 我正在尝试使用以下Javascript进行此焦点:

document.getElementById("endDateEditBox").focus();

Which chrome console and Firebug both return as undefined. 哪个chrome控制台和Firebug都返回undefined。 I feel like I am missing something very obvious here, can anyone help? 我觉得我错过了一些非常明显的东西,有人可以帮忙吗?

The focus() method does not return a value. focus()方法不返回值。 It sets the focus on the matched input, and then returns undefined ... 它将焦点设置在匹配的输入上,然后返回undefined ...

The Javascript native focus function doesn't return anything. Javascript本机焦点功能不返回任何内容。

If you expected to receive the element and use it in concatenation, you're probably confusing it with the jQuery's focus function . 如果您希望接收元素并在连接中使用它,那么您可能会将它与jQuery的焦点函数混淆。

Try this code: 试试这段代码:

<html>
<head>

<script type="text/javascript">
function setFocus()
{
    document.getElementById("endDateEditBox").focus();
}
</script>

</head>

<body onload="setFocus()">
    <form>
        Date: <input type="text" id="endDateEditBox"><br />
    </form>
</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM