在要自动获得焦点的表单域上放置autofocus属性即可,比如:
1 2 3 4 |
<form> <input name="q" autofocus> <input type="submit" value="Search"> </form> |
如果需要向下兼容一些老式浏览器,让他们也可以获得焦点,下面的代码比较实用:
1 2 3 4 5 6 7 8 9 |
<form name="f"> <input id="q" autofocus> <script> if (!("autofocus" in document.createElement("input"))) { document.getElementById("q").focus(); } </script> <input type="submit" value="Go"> </form> |
-EOF-
Pingback: Tweets that mention HTML5之自动聚焦域 » 未完待续 -- Topsy.com