为什么我需要点击两次才能触发角度为 8 和 jquery 的 onclick 事件

why do I need two click two times to trigger onclick event with angular8 and jquery

为什么我需要双击两次才能触发 angular8 和 jquery

的点击事件

//.ts

addActive1() {
$(document).ready(function () {
  $('.arrow').on('click', function () {
    $(this).siblings('.box').toggleClass('active');
    $(this).toggleClass('arrow-clicked');
  });
});

}

//.html

<a (click)="addActive1()" class="arrow shadow-lg">

经过几次搜索,我找到了适合我的情况的完美答案。 让我先解释一下我的情况,我有 3 个框,用于 Mission、Vision 等...,每个框都有固定的高度,如果我单击箭头展开它们必须适合其中 100% 的文本。 当我将它创建为静态页面时它工作正常,但是当我使用 ngFor 循环时它不起作用。

所以我找到了这个答案

$(document).on('click', '.arrow', function () {
      $(this).siblings('.box').toggleClass('active');
      $(this).toggleClass('arrow-clicked');
      console.log('Clicked!');
});

如您所见,问题是我在彼此内部使用了 2 个函数,所以它触发了 2 次,所以我将 $('.arrow') 切换为 $(文件)。并且仅在点击时使用 jQuery。