javascript - Angular document.getElementById 不能动态使用插值
问题描述
我有一个动态图像列表,我只想在用户将鼠标悬停在特定图像上时才显示不透明度。我的问题是我为每个图像动态分配了一个 id,但无法动态获取元素的属性。
我得到了 Got 插值 ({{}}) 的错误,其中表达式是预期的
<span *ngFor="let image of imagess">
<img attr.id="Image{{image.id}}"
[src]="sanitizer.bypassSecurityTrustUrl('data:'+image.mimeType+';base64, '+image.frontImage)"
onmouseover="style.opacity=.16;"
onmouseout="style.opacity=1;"
/>
<span onmouseover="document.getElementById('Image'{{image.id}}).style.opacity=.16;"> <----Right here is what I need
//Icons and other things here
</span>
</span>
解决方案
我会将其重写为 Angular 方式:
<span *ngFor="let image of images">
<img
...
#img
(mouseover)="img.style.opacity= '.16'"
(mouseout)="img.style.opacity= '1'"
/>
<span
(mouseover)="img.style.opacity= '.16'"
(mouseout)="img.style.opacity= '1'"
>Hover over me</span>
</span>
推荐阅读
- reactjs - React:兄弟组件通信问题
- ruby-on-rails - How do you send logging to Google Stackdriver with Rails/Lograge?
- python - Array changing value logic in histogram calculation
- tensorflow - FP16 甚至不比在 TensorRT 中使用 FP32 快两倍
- kubernetes - 如何在 GKE 上扩展 Kubernetes 集群?
- python - 如何计算图像上检测到的对象?
- java - 使用 JWindow 时结果不一致
- java - CursorIndexOutOfBoundsException: Index -1 requested, with a size of 163
- java - 当我在Java11中使用parallelStream时,为什么以及何时触发“线程限制超过替换阻塞的工作人员”
- c# - How to set up Firebase Push Notification with Deep Link with Unity3D