javascript - 简写 jQuery 将第一个输入集中在 div 中的表单中

标签 javascript jquery html

我想做什么

在用户点击时,我希望专注于子 div 中(唯一)表单中的第一个 inputdetailEdit.

我的 jQuery 代码有效——但它似乎是一个相当长的路要走。有简写版​​本吗?

我的代码

HTML/PHP

<div class="detailEdit">
    <form id="frm_contact">
        <span>Office:</span><br/>
        <input type="text" placeholder="Company Telephone" id="frm_tel2" name="tel2" maxlength="11" value="<?php echo $userData['tel1']; ?>" />
        <span>Mobile:</span><br/>
        <input type="text" placeholder="Mobile Number" id="frm_tel1" name="tel1" maxlength="11" value="<?php echo $userData['tel2']; ?>" />                         
        <input type="submit" value="Submit" name="submit" />
    </form>
</div>

jQuery

   $(this).children('div.detailEdit').first().children('form').first().children('input').first().focus();

最佳答案

尝试这样的事情:

$('.detailEdit').find('input:first').focus();

关于javascript - 简写 jQuery 将第一个输入集中在 div 中的表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23046764/

相关文章:

javascript - 如何向 WooCommerce 结帐页面中的自定义选择框添加验证并应用 select2 样式。

拉伸(stretch)窗口时 div 超出边界的 CSS 错误

javascript - 如何在谷歌地图中使用地址而不是经度和纬度?

javascript - jquery 子级首先删除类

javascript - 我将如何自动化这个红绿灯序列?

javascript - d3.js choropleth map -- 如何将多个属性从 CSV 映射到 JSON,以便我可以在工具提示中使用它?

javascript - IE11 是否使用哈希表来优化 document.getElementById()?

javascript - GatsbyJS 和 super 代理 : WebpackError: require is not a function

javascript - 在 Angular 动态创建的对象上调用 jQuery 函数

javascript - 读取未知属性的名称