簡體   English   中英

如何使用角度更改鼠標輸入的文本顏色

[英]how to change color of text on mouse enter using angular

我是編程的新手。 我已經嘗試過此代碼,但是@hostlistner部分無法正常工作,我使用了Bootstrap版本4

它也沒有給出任何編譯錯誤

 element.nativeElement.style.color = 'red' 

該聲明有效,但

 this.element.nativeElement.style.color = 'blue';

這個不

      import { Directive, ElementRef, HostListener } from '@angular/core';

         @Directive({
          selector: '[setmycolor]'
           })
          export class SetmycolorDirective {

          constructor(private element:ElementRef) {
          element.nativeElement.style.color = 'red';
          }


          @HostListener('onmouseenter')onMouseEnter(){
          this.element.nativeElement.style.color = 'blue';
          }

          }

應用程式中的程式碼

請嘗試以下方法:

監聽事件的名稱是的mouseenter,OnMouseEnter在 希望你明白了。:)

 import { Directive, ElementRef, HostListener } from '@angular/core';

         @Directive({
          selector: '[setmycolor]'
           })
          export class SetmycolorDirective {

          constructor(private element:ElementRef) {
          element.nativeElement.style.color = 'red';
          }


          @HostListener('mouseenter') onMouseEnter(){ //SEE HERE
          this.element.nativeElement.style.color = 'blue';
          }

          }

你的代碼是正確的,只是用了mouseenter取代OnMouseEnter在

@HostListener('mouseenter') onMouseEnter(){
          this.element.nativeElement.style.color = 'blue';
          }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM