jQuery 函数正在为模态点击返回更多事件

发布于 2025-01-19 07:32:24 字数 790 浏览 2 评论 0原文

我有一个网站,其中有三个不同的模式按钮,我编写了一个变量来获取数据层中的值,该值运行良好,但是当我第一次单击该按钮时,我收到 2 个事件,之后每次单击它都会开始加倍。

这是我的代码

function() {
  
    if ({{Click Text}} == 'STUDY DESIGN'){
  
      $(this).each(function() {
      $(this).on("click", function(){
           var imageText = $({{Click Element}}).closest('.d-md-flex').find('.image-wrapper img.image__desktop').attr("alt");
         window.dataLayer = window.dataLayer || [];
         window.dataLayer.push({
         'event': 'studyDesignInteraction',
         'action': imageText
        });
      });
  });
       return true;
     }
  }

GTM Assistant

第一次点击会返回两个事件,并且每次事件后都会加倍。怎样才能只一键申请?

I have a website where there are three different modal buttons and I wrote a variable to get the values in a data layer which is working good but when I first click on the button I get 2 events and it starts doubling up for each click afterwards.

Here is my code

function() {
  
    if ({{Click Text}} == 'STUDY DESIGN'){
  
      $(this).each(function() {
      $(this).on("click", function(){
           var imageText = $({{Click Element}}).closest('.d-md-flex').find('.image-wrapper img.image__desktop').attr("alt");
         window.dataLayer = window.dataLayer || [];
         window.dataLayer.push({
         'event': 'studyDesignInteraction',
         'action': imageText
        });
      });
  });
       return true;
     }
  }

GTM Assistant

The first click comes back with two events and it keeps doubling up after each event. How can I only apply to one click?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2025-01-26 07:32:24

$(this)您的代码是窗口

$(this).each(function() { console.log(Object.keys(this)[0])
  $(this).on("click", function() { console.log(Object.keys(this)[0])
    var imageText = $('img').closest('.d-md-flex').find('.image-wrapper img.image__desktop').attr("alt");
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      'event': 'studyDesignInteraction',
      'action': imageText
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="x" class="d-md-flex">
  <div class="image-wrapper">
    <img class="image__desktop" />
  </div>
</div>

$(this) in your code is window

$(this).each(function() { console.log(Object.keys(this)[0])
  $(this).on("click", function() { console.log(Object.keys(this)[0])
    var imageText = $('img').closest('.d-md-flex').find('.image-wrapper img.image__desktop').attr("alt");
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      'event': 'studyDesignInteraction',
      'action': imageText
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="x" class="d-md-flex">
  <div class="image-wrapper">
    <img class="image__desktop" />
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文