SweetAlert - 改变按钮的颜色

SweetAlert - Change Color of Button

我正在尝试像更改确认按钮一样更改取消按钮的颜色,但由于某些原因它似乎不起作用。

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");
    }
});

没有 default 方法可以做到这一点。但是,您可以使用自定义 css..

覆盖样式

检查此 fiddle:https://jsfiddle.net/74agy8ew/1/

您正在使用此版本的 SweetAlert:https://github.com/t4t5/sweetalert and in the source JS file (https://t4t5.github.io/sweetalert/dist/sweetalert-dev.js),没有更改取消按钮颜色的参数。

在您使用的文件中,参数为:

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
};

我可以建议您使用此版本的 SweetAlert:https://github.com/limonte/sweetalert2 因为这里提供了更改取消按钮颜色的选项。

您可以修改第一个JS文件的源代码,但是在第二个版本中它是现成的。

始终可以使用 CSS 修改按钮颜色的选项。但是如果你想使用 JS 自定义它,那么 SweetAlert2 是一个更好的选择。

您可以尝试关注

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"});

将此添加到您的 css 以覆盖 sweetalert2 样式:

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

甜蜜警报 1

在 CSS 你可以这样做:

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

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

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

如果您不希望按钮在单击时闪烁,请添加此选项。

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

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

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

希望对你有帮助:D

要自定义取消按钮,请使用 "customClass" 函数并构建您的 css 以取消按钮为目标。

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:

.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;
}

[更新 => 悬停选项]

也许它会对使用第二版的人有所帮助
https://sweetalert.js.org/guides/#installation

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

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

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

想用SweetAlert2的人,例如:

您也可以根据需要migrate from SweetAlert to 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>


正在升级到 v9.x 的 SweetAlert2。

Breaking change - rename type to 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>

不再使用 confirmButtonColor。相反,您应该通过 CSS 指定所有样式更改。作为一个有用的 shorthand,你可以设置 dangerMode: true 让确认按钮变成红色。否则,您可以在按钮对象中指定一个 class。 enter link description here

对于 Sweetalert2

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


css

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

我找到的最简单的解决方案:

将此添加到 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;
}

在 CSS 你可以这样做

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

您可以为每个按钮设置class

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

对于 sweetalert2 v11.4.0

我在js中使用

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

在css

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