【问题标题】:Setting focus on first td cell with input in table在表格中输入第一个 td 单元格
【发布时间】:2014-09-02 16:54:15
【问题描述】:

我无法将焦点设置在表格中带有输入框的第一个 td 单元格上。我使用来自this post 的解决方案将单元格内容更改为输入。现在我想将焦点放在第一个输入框上。 我的表结构是这样的

<table id="knowledgeTreeTable" class="custom">
  <tbody>
    <tr>
      <th class="">Who are the services being provided for?</th>
      <td class="">
        <input type="text" style="width: 97%;">
      </td>
    </tr>
    <tr>
     .....
    </tr>
    ..... 
   </tbody>
</table>

【问题讨论】:

    标签: jquery input focus


    【解决方案1】:

    假设您使用的是 jquery,您可以使用如下内容:

    $('#knowledgeTreeTable input').first().focus();
    

    【讨论】:

      【解决方案2】:

      这个?

      $(document).ready(function(){
          $("#knowledgeTreeTable tr:nth-of-type(1) input").focus();
      });
      

      CSS 伪类element:nth-of-type(n) 在文档中选择其类型的第 n 个元素。 由空格分隔的元素表示父子关系。所以代码说类似

      "选择一个输入,其父级是其类型的第一个并且是 ID 为 knowledgeTreeTable 的元素的子级"

      About pseudo-classes

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多