简体   繁体   English

如何使用 Angular 在鼠标悬停时找到当前元素文本?

[英]How can I find current element text on mouseover using Angular?

This is my code:这是我的代码:

 <ng-material-multilevel-menu (mouseover)="callMe($event)" mat-raised-button matTooltip={{tool}} [configuration]='config' class="nav" [items]='appItems'
            (selectedItem)="selectedItem($event)" (selectedLabel)="selectedLabel($event)">
          </ng-material-multilevel-menu>
        </nav>

My CallMe() function:我的 CallMe() function:

 callMe($event){
    console.log("Hello");
    console.log("event",$event);
  }

How can I display the label name on mouseover event?如何在鼠标悬停事件上显示 label 名称?

Solution 1解决方案 1
Using the DOM html javascript使用 DOM html javascript

<ng-material-multilevel-menu onmouseover="callMe(this)" mat-raised-button matTooltip={{tool}} [configuration]='config' class="nav" [items]='appItems'
            (selectedItem)="selectedItem($event)" (selectedLabel)="selectedLabel($event)">
          </ng-material-multilevel-menu>
        </nav>

in index.html在索引中。html

<script>
        function callMe(x) {
            console.log(x)
        }
</script>

Solution 2解决方案 2
Using typescript file使用 typescript 文件

<ng-material-multilevel-menu (mouseover)="callMe($event)" mat-raised-button matTooltip={{tool}} [configuration]='config' class="nav" [items]='appItems'
            (selectedItem)="selectedItem($event)" (selectedLabel)="selectedLabel($event)">
          </ng-material-multilevel-menu>
        </nav>

in typescript filetypescript 文件

callMe(x) {
    console.log(x.fromElement.innerHTML)
    var doc = new DOMParser().parseFromString(x.fromElement.innerHTML, "text/html");
    console.log(doc.firstChild)
    console.log(doc.firstChild.lastChild.lastChild)
  }

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

相关问题 如何在Angular2(ngx)中通过CSS选择器找到元素? - How can I find an element by css selector in Angular2 (ngx)? 如何使用 angular 中的 setAttribute 将鼠标悬停事件添加到动态生成的 html 元素 - How to add mouseover event to a dynamically generated html element by using setAttribute in angular "如何使用 Angular 2 中的日期管道测试元素的渲染?" - How can I test the rendering of an element using the date pipe in Angular 2? Angular 2+-如何在同一元素上组合mouseover,mouseout和click事件? - Angular 2+ - How to combined mouseover, mouseout and click events on the same element? 如何使用发布请求更改文本? (角度) - How can i change text using post requests? (ANGULAR) 如何根据当前组件隐藏元素(在组件内)? (我正在使用路由器) - How can I hide an element (within a component) depending on the current component? (I'm using router) 如何在我的 Observable 中找到具有最高 id 的元素<post[]>在 Angular 中?</post[]> - How can I find the element with the highest id in my Observable<Post[]> in Angular? 如何使用 angular 按数组元素排序 - How can I order by an array element with angular 如何在angular2中重新加载元素? - How can i reload element in angular2? 如何在Angular中将输入元素居中? - How can I centre an input element in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM