[英]overriding tr onclick
I've got the following code in a django template:我在 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 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 %}
However, the button onclick
events don't work, because the tr onclick
seems to override it.但是,按钮
onclick
事件不起作用,因为 tr onclick
似乎覆盖了它。 How can I prevent this from happening?我怎样才能防止这种情况发生?
please try the following:请尝试以下方法:
<html>
<body>
<table >
<tr onclick="alert('tr');">
<td><input type="button" onclick="event.cancelBubble=true;alert('input');"/></td>
</tr>
<table>
</body>
</html>
The event.cancelBubble=true will suppress the tr click event event.cancelBubble=true将抑制 tr 点击事件
Using event.stopPropagation()
will do the trick!使用
event.stopPropagation()
就可以了!
An other approach: Create a separate row for the button so it wont inherit the tr onclick event of the main row.另一种方法:为按钮创建一个单独的行,这样它就不会继承主行的 tr onclick 事件。 Something like this:
像这样的东西:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.