简体   繁体   English

在离子按钮上触发模糊事件

[英]Trigger blur event on ion-button

I am trying to have the focus be removed from an ion-button based on certain conditions. 我试图根据某些条件将焦点从离子按钮上移开。 I use CSS to change the colour of the button once it is selected, and I want this "active" state removed conditionally. 选择按钮后,我将使用CSS更改按钮的颜色,并且希望有条件地删除此“活动”状态。

ion-button.ion-color-primary:active {
    --ion-color-base: #fff !important;
    --ion-color-contrast: var(--ion-color-primary) !important;
    border: 1px solid var(--ion-color-primary);
}

In short, the functionality I am after is something like: 简而言之,我追求的功能是这样的:

If the user chose an answer and the answer is incorrect, then it should no longer have focus. 如果用户选择了一个答案而答案不正确,则它应该不再具有焦点。

I have tried the below code, based on another post that shows how to manage focus with an input field, but it does not seem to work and generates an "is not a function" error: 我已经尝试了下面的代码,该代码基于另一篇文章,该文章显示了如何使用输入字段管理焦点,但是它似乎不起作用,并生成“不是函数”错误:

HTML 的HTML

 <ion-button (click)="checkTrue()" color="primary"id="next" #next>Next</ion-button> 

TypeScript 打字稿

@ViewChild('next') nextBtn: any;

checkTrue(){
   if(conition === true){
      this.nextBtn.blur();
   }
}

I also noticed in the documentation that there is an event called "ionBlur" on a button, so you can set things in motion on a blur event on a button. 我还在文档中注意到,按钮上有一个名为“ ionBlur”的事件,因此您可以在按钮上的模糊事件上设置动态对象。 I am not finding a way of triggering the blur event however. 我没有找到触发模糊事件的方法。

Finally, I came across this post that was doing something similar. 最后,我遇到了这篇类似的文章。 I tried applying it without any luck. 我尝试应用它没有任何运气。 Before even adding any of the suggested code, I noticed that the "active" class briefly gets added to the button. 在甚至没有添加任何建议的代码之前,我注意到“ active”类已短暂添加到按钮中。 It however gets removed practically imminently, but the styling for the button is still displaying as having focus. 但是实际上几乎已将其删除,但是按钮的样式仍显示为具有焦点。

Does anyone have any advice on how to remove the "active" state from the ion-button? 是否有人对如何从离子按钮上取消“激活”状态有任何建议? Any advice would be greatly appreciated! 任何建议将不胜感激!

You can do something like this, 你可以做这样的事情,

<ion-button (click)="checkTrue()" [ngClass]="{'conition' : active}" color="primary"id="next" #next>Next</ion-button> 

You just need to do this in HTML file and in ts in checkTrue function you need to just check connition variable's value and set it true or false. 您只需要在HTML文件中执行此操作,而在checkTrue函数中的ts中,您只需要检查条件变量的值并将其设置为true或false即可。

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

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