简体   繁体   English

如何更改Ionic2中Alert中按钮的颜色

[英]How to change the color of buttons in Alert in Ionic2

Is it possible to change the color of buttons of Alert (Ok, Cancel) in Ionic2? 是否可以在Ionic2中更改Alert(Ok,Cancel)按钮的颜色? Can we use cssClass property to give color to buttons? 我们可以使用cssClass属性为按钮赋予颜色吗?

.buttonCss{
    button{
        color:#e74c3c !important;
    }
}

The above code gives the same color to the both Ok and Cancel buttons like the below image 上面的代码为Ok和Cancel按钮提供了相同的颜色,如下图所示 在此输入图像描述

But I need to get the following result(Both button shoulb in in different color), 但是我需要得到以下结果(两个按钮都是不同的颜色), 在此输入图像描述

Any help is appreciated...! 任何帮助表示赞赏......!

Edit 1: Added showAlert() function 编辑1:添加了showAlert()函数

    showAlert(title, message, yesHandler, noHandler, caller) {
    let alert = this.alertCtrl.create({
        title: title || "Please Confirm",
        message: message,
        cssClass:'buttonCss',
        buttons: [
            {
                text: 'Exit',
                handler: () => yesHandler(caller)
            },
            {
                text: 'Cancel',
                role: 'cancel',
                handler: () => noHandler(caller)
            }
        ]
    });
    alert.present();
}

1) First option, just using a class for the alert, and a css style rule for each button 1)第一个选项,只使用警报类,每个按钮使用css样式规则

showAlert(title, message, yesHandler, noHandler, caller) {
    let alert = this.alertCtrl.create({
        title: title || "Please Confirm",
        message: message,
        cssClass:'buttonCss',
        buttons: [
            {
                text: 'Exit',
                handler: () => yesHandler(caller)
            },
            {
                text: 'Cancel',
                role: 'cancel',
                handler: () => noHandler(caller)
            }
        ]
    });
    alert.present();
}

And then: 接着:

.buttonCss {

    button.alert-button:nth-child(1){
      color: red;
    }

    button.alert-button:nth-child(2){
      color: green;
    }
}

This way the first button ( nth-child(1) ) will be red and the second button ( nth-child(2) ) will be green . 这样第一个按钮( nth-child(1) )将为red ,第二个按钮( nth-child(2) )将为green

2) Second option, using a class for the alert, and adding the cssClass property to each button, in order to use that custom class on each button 2)第二个选项,使用类的警报,并将cssClass属性添加到每个按钮,以便在每个按钮上使用该自定义类

showAlert(title, message, yesHandler, noHandler, caller) {
    let alert = this.alertCtrl.create({
        title: title || "Please Confirm",
        message: message,
        cssClass:'buttonCss',
        buttons: [
            {
                text: 'Exit',
                cssClass: 'exit-button',
                handler: () => yesHandler(caller)
            },
            {
                text: 'Cancel',
                role: 'cancel',
                cssClass: 'cancel-button',
                handler: () => noHandler(caller)
            }
        ]
    });
    alert.present();
}

And then: 接着:

.buttonCss {

    button.alert-button.exit-button{
      color: red;
    }

    button.alert-button.cancel-button{
      color: green;
    }
}

You need to give each button a class, then when assigning a class to each button, you can change the colour of each individual button. 您需要为每个按钮指定一个类,然后在为每个按钮指定一个类时,您可以更改每个按钮的颜色。 Also you can add hover effects to change the colour on hover as well. 您还可以添加悬停效果以更改悬停时的颜色。

you have options to add custom class for button using cssClass 您可以使用cssClass为按钮添加自定义类

showAlert(title, message, yesHandler, noHandler, caller) {
let alert = this.alertCtrl.create({
    title: title || "Please Confirm",
    message: message,
    cssClass:'buttonCss',
    buttons: [
        {
            text: 'Exit',
            handler: () => yesHandler(caller)
        },
        {
            text: 'Cancel',
            cssClass: 'customClass',
            role: 'cancel',
            handler: () => noHandler(caller)
        }
    ]
});
alert.present();

} }

In Css: 在Css中:

.customClass{
  color:#e74c3c !important;
 }

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

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