繁体   English   中英

如何启动 onclick function 并使其启动另一个 function?

[英]How do I launch an onclick function and make it launch another function?

I need to launch an onclick function when someone clicks a button on my site (has the id of "rum_sst_tab") and then launch another function that counts a google conversion ( gtag_report_conversion() )

我已经为 onclick 尝试了一些 jQuery 选项,但它们对我不起作用。 这是一个 wordpress 站点,我正在尝试打开 onclick 的 ID,位于 WordPress 插件中 - 不确定这是否会使事情复杂化。

document.getElementById("rum_sst_tab").onclick = gtag_report_conversion();

这是连接到 gtag function 的代码的另一部分:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-58161224-5"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'AW-72452xxxx');
</script>

我也在控制台中收到此错误:

(索引):1251 未捕获类型错误:无法设置 null 的属性“onclick”

如果您尝试在解析之前访问 DOM, getElementById()将返回 null,因此请确保在此之后调用您的代码。 否则添加点击事件很简单......

<!DOCTYPE html>
<html>
  <head>
    <title>so test</title>
  </head>
  <body>
    <button type="button" id="test-div">click me!</button>
    <script>
      function testFunc() {
        console.log('doing the test!');
      }
      document.getElementById('test-div').onclick = testFunc;
    </script>
  </body>
</html>

调用 function 的正确方法是:

document.getElementById('rum_sst_tab').onclick = 
       function(){
             gtag_report_conversion();
       }

(看: 使用 HTML 中的提交按钮调用 function

笔记:

建议您等待 9-10 小时后再检查您的转化率。 这是 Google Analytics 和 Google Ads 开始合作可能需要多长时间。

(在https://wpforms.com/how-to-track-form-submissions-as-google-adwords-conversions/的最终想法中)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM