gpt4 book ai didi

javascript - 如何从按钮中删除类并在 sweetalert2 中添加自定义类?

转载 作者:行者123 更新时间:2023-12-02 23:19:21 35 4
gpt4 key购买 nike

我正在使用 sweetalert2 插件。它工作正常,但现在我想删除按钮类并添加我自己的类。

class=swal2-confirm swal2-styled 但我需要 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'
)
}
})

最佳答案

您可以使用customClass来做到这一点:

customClass: {
confirmButton: 'my-confirm-button-class'
},

或者,您可以在BeforeOpen 上切换类(class):

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>

is there a way where i can change permanent classes

我建议添加您本地的 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>

关于javascript - 如何从按钮中删除类并在 sweetalert2 中添加自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57019404/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com