ruby-on-rails 如何控制daisyUI tailwind模式默认打开

chhkpiq4  于 2023-02-17  发布在  Ruby
关注(0)|答案(3)|浏览(195)

我设置了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>

那么我们如何配置模态视图呈现visible呢?
非常感谢

v9tzhpje

v9tzhpje1#

另一种方法是操作modal div之前插入的input checkbox元素。如果你控制台记录这个元素的值,你会注意到当模型打开时它被设置为“true”,当模型关闭时它被设置为“false”。
如果您希望默认打开模态,可以用途:

document.getElementById('my-modal-4').checked = true;

当呈现页面/组件时

f87krz0w

f87krz0w2#

只需按照modal-id动态添加/删除属性,JavaScript的.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>
hrirmatl

hrirmatl3#

我知道这是个老问题了但也许能在将来帮上忙,

<input
        checked={true}
        type="checkbox"
        id="add-new-phone-number"
        className="modal-toggle"
      />

如果使用react,则可以将输入的选中属性绑定到状态

相关问题