SweetAlert - 如何禁用初始打开时的自动对焦?

问题描述 投票:0回答:1

我正在使用 SweetAlert 并且我有像这样的示例的自定义按钮:

swal("A wild Pikachu appeared! What do you want to do?", {
  buttons: {
    cancel: "Run away!",
    catch: {
      text: "Throw Pokéball!",
      value: "catch",
    },
    defeat: true,
  },
  onOpen: function() { console.log("Test") } // this doesn't work
})
.then((value) => { ... });

但是,在初次打开时,它会聚焦到最后一个按钮。有没有办法完全取消这种自动对焦,以便在初次打开时,它不会聚焦在任何按钮上?

javascript sweetalert
1个回答
5
投票

正如评论中已经建议的,SweetAlert2是更灵活的解决方案,我建议使用它。

使用

didOpen
参数和
getConfirmButton()
方法,您可以实现所需的行为:

Swal.fire({
  input: 'text',
  inputPlaceholder: 'I will not be autofocuses',
  didOpen: () => Swal.getConfirmButton().focus()
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> 

© www.soinside.com 2019 - 2024. All rights reserved.