简体   繁体   English

如何在 DaisyUI 折叠标题中获取按钮的单击事件?

[英]How can I grab a button's click event in a DaisyUI Collapse title?

I'm trying to do something like this: https://stackblitz.com/edit/daisyui-sveltekit-furm6v?file=src%2Froutes%2F%2Bpage.svelte我正在尝试做这样的事情: https://stackblitz.com/edit/daisyui-sveltekit-furm6v?file=src%2Froutes%2F%2Bpage.svelte

Here's the code from it:这是它的代码:

<h1 class='text-xl'>Daisy attempt</h1>

<div class="collapse collapse-arrow">
  <input type="checkbox" /> 
  <div class="collapse-title text-xl font-medium">
    <span class=pr-4>Click me to show/hide content</span>
    <button on:click|stopPropagation={() => alert('saved')} class='btn btn-error'>save changes</button>
  </div>
  <div class="collapse-content"> 
    <p>hello</p>
  </div>
</div>

<hr class=mt-4>

<h1 class='text-xl'>Native components solution</h1>

<details>
  <summary>
    <span class=pr-4>Click me to show/hide content</span>
    <button on:click|stopPropagation={() => alert('saved')} class='btn btn-error'>save changes</button>
  </summary>
  
  details
</details>

<style>
summary:hover {
  cursor: pointer;
}
</style>

I'm not sure if/how to get the button's click handler to run.我不确定是否/如何让按钮的点击处理程序运行。 Any advice?有什么建议吗?

Thanks谢谢

I've tried using z-index but I suppose that's only for display not event handling.我试过使用 z-index 但我想那只是为了display而不是事件处理。

I've tried stopPropagation on the button but that didn't work either.我已经在按钮上尝试了stopPropagation ,但那也没有用。

So the issue right now is that the input element is covering the title div, which is why your clicks are not reaching the button.所以现在的问题是input元素覆盖了标题 div,这就是为什么您的点击没有到达按钮。 To fix this we can just change the order a bit and mess with the pointer events.为了解决这个问题,我们可以稍微改变一下顺序并弄乱指针事件。

.collapse-title {
  z-index: 10;
  pointer-events: none;
}
.collapse-title button {
  pointer-events: all;
}

this will make sure that the title covers the input, but that clicks fall though to the input while not falling through the button.这将确保标题覆盖输入,但点击会落入输入而不是落入按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 中,如何从不同组件的事件处理程序触发自定义按钮的单击事件? - In React how can I trigger a custom button's click event from a different compoennt's event handler? 除了 URL 之外,如何让这个“共享”按钮获取当前 URL 的 HTML 标题? - How do I get this “Share” button to grab the current URL's HTML title in addition to URL? 无法弄清楚如何获取按钮单击事件的值并将其粘贴到文本输入字段中 - Can't figure out how to grab value of button click event and paste it into a text input field 如何控制 daisyUI 顺风模式默认打开 - How can I control daisyUI tailwind modal open as default 如何从嵌套数组中获取特定标题并设置为折叠? - How can i get a specific title's from a nested array and set in a collapse? 当我通过 JS 单击按钮时,如何折叠(来回)一个部分? - how can I collapse (forth and back) a section when I click on a button through JS? 如何从对象中获取标题并将其应用于视频? - How can I grab the title from object and apply it to my video? 单击按钮的标题或按钮图标后,按钮的事件不起作用 - Button's event doesn't work after click on button's title or button's icon 如何使用onload事件转换按钮单击? - how can I convert a button click with an onload event? 如何拦截推特按钮单击事件? - How can I intercept the tweet button click event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM