zoukankan      html  css  js  c++  java
  • How to set the default input focus on a field in an HTML web form

     

    When I create a web-based user interface I tend to be a fanatic about making sure that the first input field on a form gets input focus when the form is first displayed. It always drives me crazy when I go to a web form that requires text input, but the developer of the page hasn't taken the time to put the default focus in the first field of the form.

    So ... after looking around at some HTML/JSF/Struts/JSP code I've written over the last few years, the following JSF example shows the most concise way I know of setting default input focus on an HTML form field:

    <h:form id="contact_add">
    <table>
    <tr>
      <td>Contact Name:</td>
      <td><h:inputText id="contact_name" size="20" value=""/></td>
      <td><h:message for="contact_name"/></td>
    </tr>
    <!-- the rest of your form would be here ... ->
    </table>
    </h:form>
    
    <!-- code to set the default input focus -->
    <script>
      document.getElementById('contact_add:contact_name').focus();
    </script>
    

      

    In this example I use the 

    document.getElementById('contact_add:contact_name').focus(); 
    

      

    syntax to put the default input focus in the textfield named contact_add:contact_name. When using JSF, this is the name JSF ends up applying to my "first name" textfield. In your code just use the name of your HTML component.

  • 相关阅读:
    Kosaraju算法---强联通分量
    Java和C++的区别
    嵌入式面试题汇总
    tabbar 之 基本结构搭建
    路由 之 再识
    常见问题 之 vue项目中使用less报错
    路由 之 初识
    VueCLI3创建项目和目录结构与配置信息详解
    runtime-compiler 和 runtime-only的区别
    vueCLI2 之 目录结构
  • 原文地址:https://www.cnblogs.com/hephec/p/4586798.html
Copyright © 2011-2022 走看看