Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重写tr onclick_Javascript_Html_Django Templates - Fatal编程技术网

Javascript 重写tr onclick

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

我在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 %}
{items%]中的项的%
{{item.name}
{{item.date}
编辑
去除
{%endfor%}

但是,按钮
onclick
事件不起作用,因为tr
onclick
似乎覆盖了它。如何防止这种情况发生?

请尝试以下操作:

<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
点击