Jquery - 通过父级输入 focus() 并查找嵌套元素类

标签 jquery html

我有这个 html。我如何使用 JQuery 获取输入字段 id="studio123"点击 <a class="mm-subopen" href="#clickMe"></a> 即可获得焦点

<li class="img no-arrow join">
        <span class="user-wrapper">
            <a class="mm-subopen" href="#clickMe"></a>
        </span>
        <ul class="chat-messages" id="studio">
            <div class="chat-input">
                <input type="text" class="form-control" id="studio123" placeholder="Type your message" />
            </div>
        </ul>
    </li>

我在想这样的事情:

$('.mm-subopen').click(function () {
    //find this.class parent, parent (witch would be:  <li class="img no-arrow join">)
    //and then trough: <li class="img no-arrow join"> find the input in same scope and get it focused
});

如有任何帮助,我们将不胜感激!

最佳答案

您可以替换链接

<a class="mm-subopen" href="#clickMe"></a>

带有标签:

<label class="mm-subopen" for="studio123"></label>

并且不需要 JavaScript。或者,如果您确实需要 JavaScript,请尝试以下操作:

$('.mm-subopen').click(function () {
    $(this).closest('li').find('input').focus();
});

关于Jquery - 通过父级输入 focus() 并查找嵌套元素类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701239/

相关文章:

javascript - Javascript创建文件上传字段什么时候会失败?

python - 将 python 源代码解析为 html - 引号

jquery - 如何在 Bootstrap 导航栏下方显示链接到的部分

html - 图片左侧文字

javascript - 使用 jQuery XPath 选择器

jquery - 当目标类的多个实例位于同一页面上时,如何使用 jQuery 计算单个 block 中的子级数量

jQuery on mouseover 在 mouseout 恢复时做一些事情

javascript - 打印窗口不会在 chrome 最新版本中打印整页

javascript - 如何使用纯 javascript 在任何网站中动态包含 jQuery

javascript - Markdown 子集