当按钮被点击时,如何更改输入框的背景颜色?

huangapple go评论81阅读模式
英文:

How to change the background color of input field when a button is clicked?

问题

我想在单击按钮时将输入字段更改为更改颜色。额外信息:单击按钮时按钮的值本身也会更改。

这是我现在拥有的HTML:

<h2 class="label-custom">Tag</h2>
<input class="input" [readonly]="isReadOnly" formControlName="tag" [placeholder]="'Enter your tags' | transloco">
<button type="alter-button" class="alter-button" (click)="alter()">{{btnVal}}</button>

alter()函数的作用是使字段只读并更改按钮名称。我还可以根据btnVal当前包含的值更改输入字段的背景颜色,因为alter()函数在每次单击时都会调用。是否有办法在包含alter()函数的TypeScript文件中更改输入字段的背景色?

英文:

I would like to change my input field to change color when I click on a button. For extra information: the button's value itself changes when it is clicked.

This is the HTML I have right now:

&lt;h2 class=&quot;label-custom&quot;&gt;Tag&lt;/h2&gt;
    &lt;input class=&quot;input&quot; [readonly]=&quot;isReadOnly&quot; formControlName=&quot;tag&quot; [placeholder]=&quot;&#39;Enter your tags&#39; | transloco&quot;&gt;
    &lt;button type=&quot;alter-button&quot; class=&quot;alter-button&quot; (click)=&quot;alter()&quot;&gt;{{btnVal}}&lt;/button&gt;

All the alter() function does is make the field readonly and change the button name. I can also change the color of the input field depending on what value the btnVal currently holds since the alter() function is called with each click. Is there a way I can change the background of the input field within the typescript file containing the alter() function?

答案1

得分: 0

你可以将alter()函数修改为如下内容:

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

@Component({
  selector: 'app-root',
  template: `
    <h2 class="label-custom">Tag</h2>
    <input class="input" [readonly]="isReadOnly" formControlName="tag" [placeholder]="'Enter your tags' | transloco">
    <button type="alter-button" class="alter-button" (click)="alter()">{{btnVal}}</button>
  `
})
export class AppComponent {
  isReadOnly = false;
  btnVal = 'Alter';

  constructor(private elementRef: ElementRef) {}

  alter() {
    this.isReadOnly = !this.isReadOnly;
    this.btnVal = this.isReadOnly ? 'Revert' : 'Alter';

    const inputElement = this.elementRef.nativeElement.querySelector('.input');
    inputElement.style.backgroundColor = this.isReadOnly ? 'lightgray' : 'white';
  }
}
英文:

You can modify the alter() function to something like this

import { Component, ElementRef } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-root&#39;,
  template: `
    &lt;h2 class=&quot;label-custom&quot;&gt;Tag&lt;/h2&gt;
    &lt;input class=&quot;input&quot; [readonly]=&quot;isReadOnly&quot; formControlName=&quot;tag&quot; [placeholder]=&quot;&#39;Enter your tags&#39; | transloco&quot;&gt;
    &lt;button type=&quot;alter-button&quot; class=&quot;alter-button&quot; (click)=&quot;alter()&quot;&gt;{{btnVal}}&lt;/button&gt;
  `
})
export class AppComponent {
  isReadOnly = false;
  btnVal = &#39;Alter&#39;;

  constructor(private elementRef: ElementRef) {}

  alter() {
    this.isReadOnly = !this.isReadOnly;
    this.btnVal = this.isReadOnly ? &#39;Revert&#39; : &#39;Alter&#39;;

    const inputElement = this.elementRef.nativeElement.querySelector(&#39;.input&#39;);
    inputElement.style.backgroundColor = this.isReadOnly ? &#39;lightgray&#39; : &#39;white&#39;;
  }
}

huangapple
  • 本文由 发表于 2023年2月18日 00:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定