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 %> 响应,这意味着它没有看到我正在单击的实际元素。它应该显示“崩溃”。我已经尝试为 #collapse 设置 z-index:9999 但我仍然无法让 jQuery 注意到它。有什么想法吗?
谢谢!
最佳答案
使用target attribute的 event获取触发事件的实际元素
$(document).on('click', ".t_box", function (e) {
var id = e.target.id
alert(id);
});
关于javascript - jQuery 识别被点击的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20604281/