简体   繁体   English

检测外部点击文本区域

[英]Detect click outside textarea

When the user enters text in my textarea, my window changes color. 当用户在我的文本区域中输入文本时,我的窗口会更改颜色。 I want that when it clicks outside the textarea, it replaces color as before. 我希望当它在文本区域之外单击时,像以前一样替换颜色。

<textarea class="chat-input"
              id="textarea"
              rows="2" cols="50"
              (keyup)="detectTextarea($event)">
</textarea>


detectTextarea(event: any): any {
   this.changeColor = true;
   var textarea = document.getElementById("textarea");
}

/////////UPDATE//////////// It does not work : ///////// UPDATE ////////////不起作用:

detectTextarea(event: any): any {
  var textarea = document.getElementById("textarea");

  textarea.addEventListener("focus", function( event ) {
    this.changeColor = true;
  }, true);
  textarea.addEventListener("blur", function( event ) {
    this.changeColor = false;
  }, true);
}

Change it like this: 像这样更改它:

<textarea class="chat-input"
    id="textarea"
    rows="2" cols="50"
    (focus)="func()"
    (blur)="otherFunc()"
    (keyup)="detectTextarea($event)">
</textarea>

and then: 接着:

func() {
    this.changeColor = true;
  }
  otherFunc() {
    this.changeColor = false;
  }
}

您可以收听blur事件,以了解用户何时退出文本区域。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM