2013-12-16 85 views
1

jQuery代码:jQuery的识别点击子元素

$(document).on('click', ".t_box", function(){ 
    var id = $(this).attr('id'); 

    alert(id); 

}); 

HTML代码:

 <div class="t_box" id="<%= t.id %>"> 
      <div class="tname"> 
       <div id="collapse"> 
       <%= form_tag('http://localhost:3000/ts/collapse_ts', :method => 'post', :remote => true) do %> 
        <%= hidden_field_tag(:t_id, t.id) %> 
        <%= image_submit_tag "collapse.png" %> 
       <% end %> 
       </div> 

每当我点击我的页面上的DIV #collapse,我的警觉与<%= T响应。 ID%>,这意味着它没有看到我点击的实际元素。它应该显示'崩溃'。我曾尝试设置z-index:9999 for #collapse,但我仍然无法通过jQuery注意到它。有什么想法吗?

谢谢!

回答

4

使用eventtarget attribute获得触发事件的实际元素

$(document).on('click', ".t_box", function (e) { 
    var id = e.target.id 
    alert(id); 
}); 
+0

[的jsfiddle](http://jsfiddle.net/avZfZ/ ) –

+0

这很好,谢谢! – jamesdlivesinatree

1

试试这个:

$(document).click(function (e) { 
     alert(e.target.id); // The id of the clicked element 
    });