我有这个HTML。当点击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
});
任何帮助将不胜感激!
答案 0 :(得分:2)
您可以替换链接
<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();
});
答案 1 :(得分:1)
这就是你的想法之后
$('.mm-subopen').click(function () {
$(this).parents("li").find(":input").focus();
});
这是一个JSFIDDLE