如何控制 daisyUI tailwind 模式默认打开

Ymo*_* Wu 6 javascript ruby-on-rails modal-dialog tailwind-css daisyui

我设置了 daisyUI 但不知道如何按条件控制模态

我认为与 flowbite 或 bootstrap 类似 https://flowbite.com/docs/components/modal/

但是daisyUI还没有实现隐藏类,并且有

库中的模态开放方法

https://daisyui.com/components/modal/

<link href="https://cdn.jsdelivr.net/npm/daisyui@2.13.6/dist/full.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>

<!-- The button to open modal -->
<label for="my-modal-4" class="btn modal-button">open modal</label>

<!-- Put this part before </body> tag -->
<input type="checkbox" id="my-modal-4" class="modal-toggle">
<label for="my-modal-4" class="modal cursor-pointer">
  <label class="modal-box relative" for="">
    <h3 class="text-lg font-bold">Congratulations random Interner user!</h3>
    <p class="py-4">You've been selected for a chance to get one year of subscription to use Wikipedia for free!</p>
  </label>
</label>
Run Code Online (Sandbox Code Playgroud)

那么我们如何配置当前可见的模态视图呢?

多谢

小智 12

另一种方法是通过操作在模态 div 之前插入的输入复选框元素。如果您通过控制台记录此元素的值,您会注意到模型打开时它设置为“true”,关闭时设置为“false”。

如果您希望默认打开模式,您可以使用:

document.getElementById('my-modal-4').checked = true;
Run Code Online (Sandbox Code Playgroud)

当页面/组件呈现时


小智 6

我知道这是一个有点老的问题,但可能会对将来的人有所帮助,

      <input
        checked={true}
        type="checkbox"
        id="add-new-phone-number"
        className="modal-toggle"
      />
Run Code Online (Sandbox Code Playgroud)

如果您使用react,您可以将输入的检查属性绑定到您的状态


Ymo*_* Wu 0

只需通过 javascript 跟随 modal-id 动态添加/删除属性 '.modal-open' 类即可完成

    <label for="my-modal-4" class="btn modal-button modal-open">open modal</label>

<!-- Put this part before </body> tag -->
<input type="checkbox" id="my-modal-4" class="modal-toggle">
<label for="my-modal-4" class="modal cursor-pointer">
  <label class="modal-box relative" for="">
    <h3 class="text-lg font-bold">Congratulations random Interner user!</h3>
    <p class="py-4">You've been selected for a chance to get one year of subscription to use Wikipedia for free!</p>
  </label>
</label>
Run Code Online (Sandbox Code Playgroud)

  • 您好,这意味着在您的模态中添加 modal-open 类可以像 daisyUI 官方支持一样打开模态视图,并且当您需要关闭模态时,只需删除属性 '.modal-open' (2认同)