Home > Article > Web Front-end > HTML5 practice and analysis form - automatically obtain the focus attribute (autofocus attribute)
HTML5 has added many new functions to the form. Menglong Station will slowly introduce them to you. Today, I will introduce to you the newly added attributes related to focus in the HTML5 form.
HTML5 adds the autofocus attribute. In browsers that support it, as long as this attribute is set, the focus can be automatically moved to the corresponding field without using JavaScript to dynamically obtain focus. A small example is as follows:
HTML code
<input type="text" value="梦龙小站" autofocus />
Preview effect
In order to ensure that the above code runs normally in the browser where autofocus is set, we Whether this property is set must be detected in JavaScript. If set, there is no need to call the focus() method.
JavaScript code
function load(){ var oInp = document.getElementById("inp"); if(oInp.autofocus != true){ oInp.focus(); } } window.addEventListener('load',load, false);
HTML code
<input type="text" value="梦龙小站" autofocus />
Preview effect