Javascript jquery关注负载

Javascript jquery关注负载,javascript,jquery,focus,Javascript,Jquery,Focus,如何设置html元素的焦点 我使用document.getElementById('ID_HTML_wanted').focus()但我的html元素“ID\u html\u想要”不是焦点。我使用jquery api尝试将您的代码包装到此代码中,以便它在DOM就绪后执行 $(function(){ //your code }); 因此,它将成为 $(function(){ document.getElementById('ID_HTML_wanted').focus(); })

如何设置html元素的焦点


我使用
document.getElementById('ID_HTML_wanted').focus()但我的html元素“ID\u html\u想要”不是焦点。我使用jquery api尝试将您的代码包装到此代码中,以便它在DOM就绪后执行

$(function(){
    //your code
});
因此,它将成为

$(function(){
    document.getElementById('ID_HTML_wanted').focus();
});
但是,您的元素没有.focus()方法,如果您想真正使用jQuery的方法,请使用

$(function(){
    $("#ID_HTML_wanted").focus();
});

抱歉,在DOM就绪时,我实际上忽略了设置焦点:

$( document ).ready(function() {
  $("#ID_HTML_wanted").focus();
});
的以下三种语法都是等效的:

$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)

愚蠢的是,当DOM就绪时,我忽略了设置焦点:$(document.ready(function(){document.getElementById('ID_HTML_-wanted').focus();});是的,我很愚蠢,当DOM准备就绪时我忽略了设置焦点:
$(document).ready(function(){$(“#ID_HTML_所需”).focus()})