[英]How to get style from click event target?
在角度材质表中,第一个单元格的左边缘有24px的填充:
td.mat-cell:first-of-type, td.mat-footer-cell:first-of-type, th.mat-header-cell:first-of-type {
padding-left: 24px;
}
我怎么才能得到它? ev.target.style.paddingLeft
为空。
您可以通过将DOM元素作为模板参数来实现所需的目标:
<th mat-header-cell *matHeaderCellDef #parameter (click)="cellClick($event, parameter)" > No. </th>
cellClick(ev: MouseEvent, element: HTMLElement) {
console.log(element.offsetWidth);
}
window.getComputedStyle()
方法用于检索元素的当前实际属性。 window.getComputedStyle(myElement).paddingLeft
将为您提供元素的实际左侧填充(末尾带有“ px”)。 为了计算元素的宽度+左填充,请使用以下命令:
+window.getComputedStyle(myElement).paddingLeft.slice(0, -2) +
+window.getComputedStyle(myElement).width.slice(0, -2);
如果宽度是固定的并且从不改变,则只需使用myElement.width
而不是getComputerStyle()
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.