SweetAlert - 改变按钮的颜色

时间:2016-10-03 17:20:41

标签: javascript sweetalert

我尝试更改取消按钮的颜色,就像我可以用于确认按钮一样,但它似乎因某种原因无法正常工作。

(见https://jsfiddle.net/x20ra1o1/))

代码是:

     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: enter code here false
     }, function(isConfirm){
   if (isConfirm) {  
   swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
  } else { 
    swal("Cancelled", "Your imaginary file is safe :)", "error");  
 } 
});

12 个答案:

答案 0 :(得分:15)

您正在使用此版本的SweetAlert:https://github.com/t4t5/sweetalert并且在源JS文件(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是一个更好的选择。

答案 1 :(得分:4)

您可以尝试以下。

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

答案 2 :(得分:3)

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

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

答案 3 :(得分:2)

要使用自定义的“取消”按钮,请使用“ 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;
}

答案 4 :(得分:2)

[更新=>悬停选项]

也许会对使用此版本的人有所帮助 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
}
...

答案 5 :(得分:1)

没有default方式这样做。但是,您可以使用自定义css覆盖样式..

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

答案 6 :(得分:1)

Sweet Alert 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

答案 7 :(得分:1)

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

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

答案 8 :(得分:0)

对于那些想使用 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>

答案 9 :(得分:0)

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

答案 10 :(得分:0)

对于Sweetalert2

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


css

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

答案 11 :(得分:0)

在 CSS 中你可以做到这一点

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