[英]Change input border color in Ionic application with button click
I'm trying to figure out, how to change input border color in Ionic TypeScript application with button click, if I have input class="mess"
in html:我试图弄清楚,如果我在 html 中输入了
class="mess"
,如何通过按钮单击更改 Ionic TypeScript 应用程序中的输入边框颜色:
<form>
<ion-item>
<ion-input class="mess"></ion-input>
</ion-item>
</form>
with default color in .css
: .css
中的默认颜色:
.mess {
border: 2px solid #88ff00;
}
I can't find the way, how to change border
color from #88ff00;
我找不到方法,如何从
#88ff00;
更改border
颜色; to different color #95214b;
不同的颜色
#95214b;
with click:点击:
<ion-button (click)="changeBorderColor()">Change color</ion-button>
I've tried use document.documentElement.style.setProperty
in changeBorderColor()
function, but looks like I've to use some different way for result.我已经尝试在
changeBorderColor()
function 中使用document.documentElement.style.setProperty
,但看起来我必须使用一些不同的方式来获得结果。
Try this:尝试这个:
<ion-button (click)="changeBorderColor()">Change color</ion-button>
<script>
function changeBorderColor(){
document.getElementById(."mess").style.borderColor = "#95214b";
}
Next to your class="mess"
add an id="mess"
and in the script where it says getElementById="#mess"
or you can use getElementByClassName=".mess"
either should work fine.在您的
class="mess"
旁边添加一个id="mess"
并在脚本中显示getElementById="#mess"
或者您可以使用getElementByClassName=".mess"
或者应该可以正常工作。
You can try this way...你可以试试这个方法...
page.html file
页.html文件
<form>
<ion-item>
<ion-input [ngClass]="setBorderColor? 'mess' : ''"></ion-input>
</ion-item>
<ion-button (click)="changeBorderColor()">Change color</ion-button>
</form>
page.ts file
page.ts 文件
public setBorderColor: boolean = false;
constructor() { }
ngOnInit() {
}
changeBorderColor() {
this.setBorderColor = true;
}
page.scss file
page.scss 文件
.mess {
border: 2px solid #88ff00;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.