2014-11-02 25 views
1

我有这个html。Jquery - 通过父级输入焦点()并查找嵌套元素级别

$('.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 
}); 

任何帮助,将不胜感激:可我怎么用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> 

我喜欢思考的东西点击集中!

回答

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(); 
}); 
+0

我需要做的是在JS,因为'HREF =” #clickMe“'用于我的代码的许多其他部分。 Iv'e尝试了你的js解决方案,但它不起作用。我的''标签是在文档加载时动态构建的。这可能是问题吗? – user3228992 2014-11-02 16:05:55

+1

是的,这可能是一个问题。试试这个'$(document).on('click','.mm-subopen',function(){$(this).closest('li')。find('input')。focus();}) ;'。 – dfsq 2014-11-02 16:09:18

+0

奇怪,仍然不起作用。我认为你发送的最后一个会修复它。你知道这里还有什么问题吗?有什么建议么? – user3228992 2014-11-02 16:15:40

1

这是你后,我觉得

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

这里是一个JSFIDDLE