简体   繁体   English

SweetAlert - 改变按钮的颜色

[英]SweetAlert - Change Color of Button

I'm trying to change the color of the cancel button like I can for the confirm button but it doesn't seem to work for some reason.我正在尝试像更改确认按钮一样更改取消按钮的颜色,但由于某种原因它似乎不起作用。

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: "warning",
    showCancelButton: true,
    cancelButtonColor: "#DD6B55",
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: "No, cancel please!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function (isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
    } else {
        swal("Cancelled", "Your imaginary file is safe :)", "error");
    }
});

You are using this version of SweetAlert: https://github.com/t4t5/sweetalert and in the source JS file ( https://t4t5.github.io/sweetalert/dist/sweetalert-dev.js ), there is no such parameter to change color of cancel button.您正在使用此版本的 SweetAlert: https : //github.com/t4t5/sweetalert并且在源 JS 文件( https://t4t5.github.io/sweetalert/dist/sweetalert-dev.js )中,没有这样的参数来改变取消按钮的颜色。

In the file you have used, the params are:在您使用的文件中,参数是:

var defaultParams = {
  title: '',
  text: '',
  type: null,
  allowOutsideClick: false,
  showConfirmButton: true,
  showCancelButton: false,
  closeOnConfirm: true,
  closeOnCancel: true,
  confirmButtonText: 'OK',
  confirmButtonColor: '#8CD4F5',
  cancelButtonText: 'Cancel',
  imageUrl: null,
  imageSize: null,
  timer: null,
  customClass: '',
  html: false,
  animation: true,
  allowEscapeKey: true,
  inputType: 'text',
  inputPlaceholder: '',
  inputValue: '',
  showLoaderOnConfirm: false
};

May I suggest you to use this version of SweetAlert: https://github.com/limonte/sweetalert2 as here the option to change the cancel button color is present.我建议您使用此版本的 SweetAlert: https : //github.com/limonte/sweetalert2,因为这里提供了更改取消按钮颜色的选项。

You can modify the source code of the first JS file, however in the second version it is readily available.您可以修改第一个 JS 文件的源代码,但是在第二个版本中它很容易获得。

There is always the option available to use CSS to modify button colors.始终可以选择使用 CSS 来修改按钮颜色。 But if you want to make it customizable using JS, then SweetAlert2 is a better alternative.但是如果您想使用 JS 使其可定制,那么 SweetAlert2 是更好的选择。

[update => hover option] [更新 => 悬停选项]

Maybe it'll help someone who use 2nd version也许它会帮助使用第二个版本的人
https://sweetalert.js.org/guides/#installation https://sweetalert.js.org/guides/#installation

Javascript Javascript

swal({
    title: "Apakah anda yakin?",
    text: "Anda dapat mengaktifkannya lagi nanti...",
    icon: "warning",
    buttons: {
        confirm : {text:'Ubah',className:'sweet-warning'},
        cancel : 'Batalkan'
    },
}).then((will)=>{
    if(will){
        $(".onoffswitch-checkbox").prop('checked',false);
    }else{
        $("#all_petugas").click();
    }
});

CSS CSS

...
.sweet-warning{
 background-color: black;
 #or anything you wanna do with the button
}

.sweet-warning:not([disabled]):hover{
 #hover here
}
...

To have a custom Cancel button use the "customClass" function and build your css to target the cancel button.要自定义取消按钮,请使用“customClass”函数并构建您的 css 以定位取消按钮。

JavaScript: JavaScript:

swal({   
title: "Are you sure?",   
   text: "You will not be able to recover this imaginary file!",   
   type: "warning",   
   showCancelButton: true,      
   confirmButtonColor: "#DD6B55",   
   confirmButtonText: "Yes, delete it!",   
   cancelButtonText: "No, cancel plx!",   
   closeOnConfirm: false,   
   closeOnCancel: false,
   customClass: "Custom_Cancel"
}, 
function(isConfirm){   
   if (isConfirm) {     
      swal("Deleted!", "Your imaginary file has been deleted.", "success");   
   } else {     
      swal("Cancelled", "Your imaginary file is safe :)", "error");   
   } 
});

CSS: CSS:

.Custom_Cancel > .sa-button-container > .cancel {
   background-color: #DD6B55;
   border-color: #DD6B55;
}
.Custom_Cancel > .sa-button-container > .cancel:hover {
   background-color: #DD6B55;
   border-color: #DD6B55;
}

You can try following.您可以尝试以下。

SweetAlert.swal({
                        title: 'Thank you',
                        text: 'Thank you for using the quoting tool. Your Quote PDF has been downloaded. The quote has been sent to U.S. Legal for approval.',
                        type: 'warning',
                        confirmButtonText: 'Cancel',
    confirmButtonColor: "#DD6B55"});

Sweet Alert 1甜蜜警报 1

In CSS you can do this:在 CSS 中,你可以这样做:

.swal-button--confirm {
    background: #0a0;
}

.swal-button--cancel {
    background: #aaa;
}

.swal-button--danger {
    background: #a00;
}

Add this if you don't want the button flash when it clicked.如果您不希望按钮在单击时闪烁,请添加此项。

.swal-button--confirm:active {
    background: #0a0;
}

.swal-button--cancel:active {
    background: #aaa;
}

.swal-button--danger:active {
    background: #a00;
}

Hope it helps :D希望它有帮助:D

Add this to your css to override sweetalert2 style:将此添加到您的 css 以覆盖 sweetalert2 样式:

.swal2-styled.swal2-cancel{
  background-color: #dc3545 !important;
}

For those who want to use SweetAlert2 , for example:对于那些想要使用SweetAlert2 的人,例如:

You can also migrate from SweetAlert to SweetAlert2 as needed.您还可以根据需要从 SweetAlert 迁移到 SweetAlert2

 Swal.fire({ title: 'Are you sure?', text: "You won't be able to revert this! ⚠️", type: 'warning', showCancelButton: true, // Background color of the "Confirm"-button. The default color is #3085d6 confirmButtonColor: 'LightSeaGreen', // Background color of the "Cancel"-button. The default color is #aaa cancelButtonColor: 'Crimson', confirmButtonText: 'Yes, delete it!' }).then((result) => { if (result.value) { Swal.fire({ type: 'success', title: 'Deleted!', text: "Your file has been deleted.", timer: 2000, showCancelButton: false, showConfirmButton: false }) } })
 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>


Upgrading to v9.x of SweetAlert2.升级到v9.x的 v9.x。

Breaking change - rename type to icon重大更改- 将type重命名为icon

 Swal.fire({ title: 'Are you sure?', text: "You won't be able to revert this! ⚠️", icon: 'warning', showCancelButton: true, // Background color of the "Confirm"-button. The default color is #3085d6 confirmButtonColor: 'LightSeaGreen', // Background color of the "Cancel"-button. The default color is #aaa cancelButtonColor: 'Crimson', confirmButtonText: 'Yes, delete it!' }).then((result) => { if (result.value) { Swal.fire({ icon: 'success', title: 'Deleted!', text: "Your file has been deleted.", timer: 2000, showCancelButton: false, showConfirmButton: false }) } })
 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

There is no default way of doing this.没有这样做的default方式。 But, you can override the style with custom css..但是,您可以使用自定义 css 覆盖样式。

Check this fiddle: https://jsfiddle.net/74agy8ew/1/检查这个小提琴: https : //jsfiddle.net/74agy8ew/1/

The easiest solution I have found:我找到的最简单的解决方案:

add this to app.css:将此添加到 app.css:

.swal-modal {
    background-color: rgba(0, 0, 0, 0.651);
    color: white;
}
.swal-text {
    color: white;
    font-size: 33px;
}
.swal-button {
    background-color: #ff2600;
    color: #ffffff;
}

You can set the class to each button您可以为每个按钮设置类

            customClass: {
                confirmButton: 'btn btn-primary',
                cancelButton: 'btn btn-secondary',
            }

confirmButtonColor is no longer used.不再使用confirmButtonColor。 Instead, you should specify all stylistic changes through CSS.相反,您应该通过 CSS 指定所有样式更改。 As a useful shorthand, you can set dangerMode: true to make the confirm button red.作为一个有用的速记,您可以设置 riskMode: true 使确认按钮变为红色。 Otherwise, you can specify a class in the button object.否则,您可以在按钮对象中指定一个类。 enter link description here在此处输入链接描述

For Sweetalert2对于 Sweetalert2

Swal.fire({
  title: "Error!",
  text: "Image is not selected",
  icon: "error",
  confirmButtonText: "OK",
  showCloseButton:true,
});


css

.swal2-close{
 background-color:black;
 color: red;
 }

In CSS you can do this在 CSS 中你可以这样做

.sweet-alert button.cancel {
    background-color: #FE9475;
}

For sweetalert2 v11.4.0对于 sweetalert2 v11.4.0

I use in js我在js中使用

Swal.fire({
  title: 'My title',
  text: 'description',
  icon: 'success',
  buttonsStyling: false,
  customClass: {
    confirmButton: 'sweet-alert-button'
  },
})

In css css

.sweet-alert-button {
  background: #fac62d;
}

在此处输入图像描述

Change Color Of Button:改变按钮的颜色:

 .swal2-confirm {
        background-color: #008cba !important;
    }

在此处输入图像描述

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

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