简体   繁体   English

防止单击图像时父元素上的单击事件

[英]Prevent click event on parent element when image clicked

I am trying to stop a second piece of JS running as a result of the click event propagating to the parent element but I am stuck. 由于试图传播到父元素的click事件,我试图停止运行第二个JS,但是我被卡住了。

I have the following HTML for rows in a table: 我对表中的行有以下HTML:

<tr id="DEMO01-P" role="row" class="odd">
   <td><img id="DEMO1" src="../imagedir/edit.png" onclick="Edit(this.id);">Info here</td>
   <td>And here</td>
</tr>

I have a click event on the tr like this: 我在tr上有一个click事件, like所示:

  jQuery('#system-tbl tbody').on('click', 'tr', function () {
    var self = jQuery(this);
    if ( self.hasClass('selected') ) {
      self.removeClass('selected');
    } else {
      table.$('tr.selected').removeClass('selected');
      self.addClass('selected');
    }
    DO STUFF....
  });

I also do something else if the user clicks on the img inside the tr using onclick="Edit(this.id);" 如果用户使用onclick="Edit(this.id);"在tr内单击img我也会做其他事情onclick="Edit(this.id);" :

function Edit(system_id) {
    alert("Edit: " + system_id);
    window.location.href = "/go-to-page/";
};

How do I stop the tr event in the call to Edit(this.id); 如何在调用Edit(this.id);停止tr事件Edit(this.id); ? Thanks!! 谢谢!!

Use 采用

<td><img id="DEMO1" src="../imagedir/edit.png" onclick="Edit(event,this.id);">Info here</td>

and

function Edit(e,system_id) {
::
e.stopPropagation()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM