我正在使用sweetalert2插件。效果很好,但是现在我想删除按钮类并添加自己的类。
class = swal2-confirm swal2-style ,但我需要 class = swal2-confirm btn btn-success
任何解决方案表示赞赏!
Swal.fire({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, keep it,'
}).then((result) => {
if (result.value) {
Swal.fire(
'Deleted!',
'Your imaginary file has been deleted.',
'success'
)
} else if (result.dismiss === Swal.DismissReason.cancel) {
Swal.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
})
答案 0 :(得分:1)
您可以使用customClass:
customClass: {
confirmButton: 'my-confirm-button-class'
},
或者,您可以在onBeforeOpen之间切换类:
onBeforeOpen: function(ele) {
$(ele).find('button.swal2-confirm.swal2-styled')
.toggleClass('swal2-confirm swal2-styled swal2-confirm btn btn-success')
}
$('#btn').on('click', function (e) {
Swal.fire({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, keep it,',
customClass: {
//confirmButton: 'my-confirm-button-class'
},
onBeforeOpen: function(ele) {
$(ele).find('button.swal2-confirm.swal2-styled').toggleClass('swal2-confirm swal2-styled swal2-confirm btn btn-success')
}
}).then(function (result) {
if (result.value) {
Swal.fire(
'Deleted!',
'Your imaginary file has been deleted.',
'success'
)
} else if (result.dismiss === Swal.DismissReason.cancel) {
Swal.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@8.13.4/dist/sweetalert2.css">
<button id="btn">Click Me</button>
有什么方法可以更改永久班级
我建议添加您的本地CSS样式:
.swal2-styled.swal2-confirm {
.....
}
$('#btn').on('click', function (e) {
Swal.fire({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, keep it,'
}).then(function (result) {
if (result.value) {
Swal.fire(
'Deleted!',
'Your imaginary file has been deleted.',
'success'
)
} else if (result.dismiss === Swal.DismissReason.cancel) {
Swal.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
})
})
.swal2-styled.swal2-confirm {
color: #fff !important;
background-color: #28a745 !important;
border-color: #28a745 !important;
display: inline-block !important;
font-weight: 400 !important;
color: #212529 !important;
text-align: center !important;
vertical-align: middle !important;
-webkit-user-select: none !important;
-moz-user-select: none !important;
-ms-user-select: none !important;
user-select: none !important;
background-color: transparent !important;
border: 1px solid transparent !important;
padding: 0.375rem 0.75rem !important;
font-size: 1rem !important;
line-height: 1.5 !important;
border-radius: 0.25rem !important;
transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@8.13.4/dist/sweetalert2.css">
<button id="btn">Click Me</button>
答案 1 :(得分:0)
您可以使用:
buttonsStyling: false,
customClass: {
confirmButton: "btn btn-outline-danger"
}