css 在tr和td angular上调用Delete事件

jchrr9hc  于 4个月前  发布在  Angular
关注(0)|答案(2)|浏览(90)

我有这个tr和td标签都有onclick事件

<tr (click)="update(list.Item)" *ngFor="let list of List | paginate: { itemsPerPage: 10, currentPage: p };">
              
              <td style="width:5%" (click)="delete(list.Item)">  
                <a title="Delete"><i class="icon-trash"
                  style="margin-right: 10px"></i></a>
              </td>
            </tr>



delete(itemID)
  {

  }

字符串
问题是,当我点击td事件,然后也tr事件被调用,我想限制tr事件点击时,td被点击。

ncgqoxb0

ncgqoxb01#

这个问题是因为<td><tr>里面,那么每次你推你的<td>的时候<tr>事件也会被调用,你可以尝试把longpress选项放到<tr>里面,把(click)用到<td>里面
就像这样:

<tr longPress (mouseLongPress)="update(list.Item)">
  <td style="width:5%" (click)="delete(list.Item)">
    <a title="Delete"> Test </a>
  </td>
</tr>

字符串

waxmsbnn

waxmsbnn2#

您需要使用stopPropagation来阻止事件向上冒泡并触发其他事件!

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
        <table>
        <tr>
            <th class="sticky">No.</th>
            <th class="sticky">Firstname</th>
            <th class="sticky">Lastname</th>
            <th class="sticky">Age</th>
        </tr>
        <tr (click)="rowClick($event)">
            <td (click)="cellClick($event)">1</td>
            <td (click)="cellClick($event)">Jill</td>
            <td (click)="cellClick($event)">Smith</td>
            <td (click)="cellClick($event)">50</td>
        </tr>
        <tr (click)="rowClick($event)">
            <td (click)="cellClick($event)">1</td>
            <td (click)="cellClick($event)">Eve</td>
            <td (click)="cellClick($event)">Jackson</td>
            <td (click)="cellClick($event)">94</td>
        </tr>
        <tr (click)="rowClick($event)">
            <td (click)="cellClick($event)">1</td>
            <td (click)="cellClick($event)">Alfreds Futterkiste</td>
            <td (click)="cellClick($event)">Maria Anders</td>
            <td (click)="cellClick($event)">Germany</td>
        </tr>
    </table>

  `,
})
export class App {
  name = 'Angular';

  rowClick(event: Event) {
    console.log('row click');
  }
  cellClick(event: Event) {
    event.stopPropagation();
    console.log('cell click');
  }
}

bootstrapApplication(App);

字符串
stackblitz

相关问题