如何设置HTML元素的焦点?
我使用document.getElementById('ID_HTML_wanted').focus();
,但我的HTML元素“ID_HTML_wanted”没有焦点。我使用jQuery API .focus
如何设置HTML元素的焦点?
我使用document.getElementById('ID_HTML_wanted').focus();
,但我的HTML元素“ID_HTML_wanted”没有焦点。我使用jQuery API .focus
尝试将你的代码包装在这段代码中,以便它在 DOM 就绪后执行
$(function(){
//your code
});
所以它将变成
$(function(){
document.getElementById('ID_HTML_wanted').focus();
});
然而,您的元素没有 .focus() 方法。如果您确实想使用 jQuery 的 focus() 方法,则需要使用
$(function(){
$("#ID_HTML_wanted").focus();
});
$(document).ready(function(){ $(“#ID_HTML_wanted”)。focus(); });
- immobiluser$( document ).ready(function() {
$("#ID_HTML_wanted").focus();
});
.ready() 的以下三种语法是等效的:
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)