[英]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.