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

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

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

问题

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

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

相关代码:

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

VUE

  1. handleToggleClick: function (event) {
  2. if ((this.isMobileOnly && breakpoint() !== "large") || !this.isMobileOnly) {
  3. const btn = event.target;
  4. const content = btn.nextElementSibling;
  5. const parent = btn.parentElement;
  6. // 如果手风琴标题是链接或标题,则不打开/关闭手风琴项目
  7. if (btn.tagName === "A") {
  8. return;
  9. }
  10. if (parent.classList.contains(this.toggleClass)) {
  11. this.closeItem(btn, content, parent);
  12. } else {
  13. // 关闭所有其他打开的手风琴项目
  14. Array.from(this.$el.querySelectorAll(`.${this.toggleClass}`)).map(item => {
  15. this.closeItem(item.querySelector(".accordion__header"), item.querySelector(".accordion__content"), item);
  16. });
  17. this.openItem(btn, content, parent)
  18. setTimeout(() => { scrollToElement(btn) }, this.animationDuration + 1)
  19. }
  20. }
  21. },
英文:

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:

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

VUE

  1. handleToggleClick: function (event) {
  2. if ((this.isMobileOnly &amp;&amp; breakpoint() !== &quot;large&quot;) || !this.isMobileOnly) {
  3. const btn = event.target;
  4. const content = btn.nextElementSibling;
  5. const parent = btn.parentElement;
  6. // If the accordion title is a link or header, don&#39;t open/close the accordion item
  7. if (btn.tagName === &quot;A&quot;) {
  8. return;
  9. }
  10. if (parent.classList.contains(this.toggleClass)) {
  11. this.closeItem(btn, content, parent);
  12. } else {
  13. // Close all other open accordion items
  14. Array.from(this.$el.querySelectorAll(`.${this.toggleClass}`)).map(item =&gt; {
  15. this.closeItem(item.querySelector(&quot;.accordion__header&quot;), item.querySelector(&quot;.accordion__content&quot;), item);
  16. });
  17. this.openItem(btn, content, parent)
  18. setTimeout(() =&gt; { scrollToElement(btn) }, this.animationDuration + 1)
  19. }
  20. }
  21. },

答案1

得分: 0

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

  1. pointer-events: none;

现在一切都正常。

英文:

I figured this out. I added

  1. 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:

确定