我试图用jquery点击父母身份证。儿童点击获取父母身份证
我的Django的模板如下:
<table id="archive-table" class="table table-hover table-vcenter">
<thead>
<tr>
<th>Make and model</th>
<th>First registration</th>
</tr>
</thead>
<tbody>
{% for calculation in calculations %}
<tr data-archive-row class="archive-row" data-calculation-id={{ calculation.id }}>
<td>{{ calculation.first_registration }}</td>
<td>{{ calculation.body }}</td>
</tr>
{% endfor %}
</tbody>
</table>
而我的JS如下:
<script>
$(document).ready(function() {
$('#archive-table').on('click', '[data-archive-row]', function (e) {
var calculation_id = e.target.dataset['calculationId'];
alert(calculation_id)
})
});
</script>
我怎样才能不管我对孩子点击父的dataset['calculationId']
。
随着我的代码我得到undefined在alert。但是,如果我例如将data-calculation-id={{ calculation.id }}
添加到一个td,如果我然后点击它然后我得到正确的ID。
有没有办法从父母那里得到这个id,不管它是否被点击在孩子或父母身上?
这是很好的(我可能会使用'this.dataset'),但仍是要了解e.target'和'e.currentTarget'之间'区别是很重要的。 – dfsq
没错,只是我认为最好不要将纯js与jQuery混合在一起,只要我们可以用其中的一个来实现这个诀窍。 –