Javascript 重写tr onclick
我在django模板中获得了以下代码:Javascript 重写tr onclick,javascript,html,django-templates,Javascript,Html,Django Templates,我在django模板中获得了以下代码: {% for item in items %} <tr onclick="{% url collection:viewitem item_id=item.id %}"> <td>{{ item.name }}</td> <td>{{ item.date }}</td> <td> <button onclick="{% url collect
{% for item in items %}
<tr onclick="{% url collection:viewitem item_id=item.id %}">
<td>{{ item.name }}</td>
<td>{{ item.date }}</td>
<td>
<button onclick="{% url collection:edititem item_id=item.id %}" type="button">Edit</button>
<button onclick="{% url collection:removeitem item_id=item.id %}" type="button">Remove</button>
</td>
</tr>
{% endfor %}
{items%]中的项的%
{{item.name}
{{item.date}
编辑
去除
{%endfor%}
但是,按钮
onclick
事件不起作用,因为tronclick
似乎覆盖了它。如何防止这种情况发生?请尝试以下操作:
<html>
<body>
<table >
<tr onclick="alert('tr');">
<td><input type="button" onclick="event.cancelBubble=true;alert('input');"/></td>
</tr>
<table>
</body>
</html>
event.cancelBubble=true将使用
event.stopPropagation()
抑制tr click事件 另一种方法:
为按钮创建一个单独的行,这样它就不会继承主行的tr onclick事件。大概是这样的:
<tr onclick="doSomething()">
<td rowspan="2">A</td>
<td rowspan="2">B</td>
<td rowspan="2">C</td>
</tr>
<tr>
<td><button type="button">Click</button></td>
</tr>
A.
B
C
点击