我有这个 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/