限制onclick事件的选择

时间:2013-06-24 16:38:09

标签: html onclick

我有一个div,其中包含一些文本和两个按钮。我在div上有一个onclick属性,因为我想能够点击它内部的任何地方来激活一个事件,除了两个按钮。如果我按下其中一个按钮,我不希望激活该事件。我似乎无法弄清楚在哪里放“onclick”,因为它要么选择全部要么不够。有没有办法从选择中排除这两个按钮?

我的HTML现在是:

<div id="event" class="span8" onclick="preview({{ name.event_id }})">
     <strong>
    {{ name.title|truncatechars:50 }}
     </strong> <br>
    Location: {{ name.location|truncatechars:50 }} <br>
    Start Date: {{ name.start|truncatechars:50 }} <br>
    End Date: {{ name.end|truncatechars:50 }} 
    <a class="page btn btn-primary btn-small" href="/event/{{ name.event_id }}">Event Page</a>
    <button class ="page btn btn-danger btn-small" id="{{ name.event_id }}" href="#" onClick="removeEvent({{ name.event_id }}, {{ user.get_profile.id }})">
     <i id="icon" class="icon-minus-sign icon-white"></i> 
      Remove
    </button>
...
  </div>

1 个答案:

答案 0 :(得分:1)

您只需在按钮stopPropagation属性中添加对onClick的通话,就像这样:

<button class ="page btn btn-danger btn-small" id="{{ name.event_id }}" href="#" onClick="event.stopPropagation(); removeEvent({{ name.event_id }}, {{ user.get_profile.id }})">

此前已在此处回答:How to stop event propagation with inline onclick attribute?