2011-05-29 52 views
8

我有在Django模板下面的代码:压倒一切的onclick TR

{% 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 %} 

然而,按钮onclick事件不会工作,因为TR onclick似乎覆盖它。我怎样才能防止这种情况发生?

回答

15

,请尝试以下方法:

<html> 
    <body> 
     <table > 
      <tr onclick="alert('tr');"> 
       <td><input type="button" onclick="event.cancelBubble=true;alert('input');"/></td> 
      </tr> 
     <table> 
    </body> 
</html> 

event.cancelBubble =真将打压TR click事件

+1

工作的很好,谢谢 – synic 2011-05-29 21:59:24

0

使用event.stopPropagation()会做的伎俩!