【发布时间】:2014-08-01 01:25:10
【问题描述】:
我正在尝试在点击时检索元素 ID。元素是django生成的一个div:
{% for artist in artists %}
<div class="band_box_inner" id='artist_{{artist.id}}' style="background-image: url({{artist.pic_primary.url}})">
</div>
{% endfor %}
元素正确显示在页面上。我正在尝试在单击时检索元素 ID:
$('html').on('click', '.band_box_container', function() {
alert($(this).attr('id'));
});
但是,每次这个函数都会提示“未定义”。
当我使用时:
$('html').on('click', '.band_box_container', function() {
alert($(this).html());
});
我正确获取了元素的html,包括id。
为什么我的 jquery 选择器没有选择 django 生成的模板标签 elements.attributes?
【问题讨论】:
-
你能试试
$(this).id吗? -
仍然返回未定义。真正奇怪的是,如果我将点击监听器添加到包装父元素,然后使用 $(this).find('.band_box_inner').first().atr('id') 它按预期工作。 ..
标签: javascript jquery html css django