Vue允许在子组件上触发父组件的点击事件

huangapple go评论63阅读模式
英文:

Vue Allow Parent's Click Event to be Triggered on Child

问题

我有一个手风琴元素,通过按钮触发。按钮内有一些文本和一个带有 + 的 span。当点击按钮上的文本时,我的手风琴会按预期切换,但当点击带有 + 的 span 时,我会收到一个错误,并且手风琴不会切换。添加 stop.prevent 可以解决js错误,但当点击加号时什么都不会发生,而我需要它发生。

我的问题是,无论我在按钮内的何处点击,我如何允许手风琴相应地切换?

相关代码:

<ul class="accordion js-accordion">
    @{int i = 0;}
    @foreach (var item in Model.FaqEntries)
    {
        <li @(i == 0 && Model.ExpandFirstItem ? "data-open='true'" : "") id="@item.Name">
            <button type="button" v-on:click="handleToggleClick" v-on:keydown.enter="handleToggleClick">
                @item.Title
                <span v-on:click.stop.prevent>+</span>
            </button>
            <div class="rich-text">@Html.Raw(@item.Content)</div>
        </li>
        i++;
    }
</ul>

VUE

handleToggleClick: function (event) {
    if ((this.isMobileOnly && breakpoint() !== "large") || !this.isMobileOnly) {

        const btn = event.target;
        const content = btn.nextElementSibling;
        const parent = btn.parentElement;

        // 如果手风琴标题是链接或标题,则不打开/关闭手风琴项目
        if (btn.tagName === "A") {
            return;
        }

        if (parent.classList.contains(this.toggleClass)) {
            this.closeItem(btn, content, parent);

        } else {
            // 关闭所有其他打开的手风琴项目
            Array.from(this.$el.querySelectorAll(`.${this.toggleClass}`)).map(item => {
                this.closeItem(item.querySelector(".accordion__header"), item.querySelector(".accordion__content"), item);
            });

            this.openItem(btn, content, parent)
            setTimeout(() => { scrollToElement(btn) }, this.animationDuration + 1)

        }
    }
},
英文:

I have an accordion element that is triggered by a button. Inside the button I have some text and a span with a +. When clicking the text on the button my accordion toggles as expected, but when clicking the span with the + I get an error and the accordion doesn't toggle. Adding stop.prevent helps with the js error, but nothing happens when clicking the plus which I need it to do.

My question is how can I allow for the accordion to toggle accordingly no matter where inside the button I am clicking?

Relevant code:

&lt;ul class=&quot;accordion js-accordion&quot;&gt;
        @{int i = 0;}
        @foreach (var item in Model.FaqEntries)
        {
            &lt;li @(i == 0 &amp;&amp; Model.ExpandFirstItem ? &quot;data-open=&#39;true&#39;&quot; : &quot;&quot;) id=&quot;@item.Name&quot;&gt;
                &lt;button type=&quot;button&quot; v-on:click=&quot;handleToggleClick&quot; v-on:keydown.enter=&quot;handleToggleClick&quot;&gt;
                    @item.Title
                    &lt;span v-on:click.stop.prevent&gt;+&lt;/span&gt;
                &lt;/button&gt;
                &lt;div class=&quot;rich-text&quot;&gt;@Html.Raw(@item.Content)&lt;/div&gt;
            &lt;/li&gt;
            i++;
        }
    &lt;/ul&gt;

VUE

handleToggleClick: function (event) {
            if ((this.isMobileOnly &amp;&amp; breakpoint() !== &quot;large&quot;) || !this.isMobileOnly) {

                const btn = event.target;
                const content = btn.nextElementSibling;
                const parent = btn.parentElement;

                // If the accordion title is a link or header, don&#39;t open/close the accordion item
                if (btn.tagName === &quot;A&quot;) {
                    return;
                }

                if (parent.classList.contains(this.toggleClass)) {
                    this.closeItem(btn, content, parent);

                } else {
                    // Close all other open accordion items
                    Array.from(this.$el.querySelectorAll(`.${this.toggleClass}`)).map(item =&gt; {
                        this.closeItem(item.querySelector(&quot;.accordion__header&quot;), item.querySelector(&quot;.accordion__content&quot;), item);
                    });

                    this.openItem(btn, content, parent)
                    setTimeout(() =&gt; { scrollToElement(btn) }, this.animationDuration + 1)

                }
            }
        },

答案1

得分: 0

我解决了这个问题。我向 span 添加了

pointer-events: none;

现在一切都正常。

英文:

I figured this out. I added

pointer-events: none;

to the span and now all is good.

huangapple
  • 本文由 发表于 2023年7月11日 05:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定