如何将此脚本绑定到锚点's单击事件

How do I go around binding this script to an anchor's click event?

本文关键字:事件 单击 脚本 绑定      更新时间:2023-09-26

我在实现跟踪像素脚本时遇到了问题,特别是在将脚本绑定到单击事件时。

这是我从像素跟踪服务提供商那里得到的JavaScript:

<script type="text/javascript" language="javascript">    
            var __autoFirePixels = true;    
            (function() {
                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.setAttribute('async', 'true');
                    script.src = document.location.protocol+
                                '//pixel.traffiliate.com/pixel/pixels.js.php'+
                                'allsortsofapiinformation';
                    document.documentElement.firstChild.appendChild(script);
        })();
</script>

当点击呼叫按钮时,我需要触发这个代码——在登录页中有两个这样的按钮。

我觉得解决方案指日可待,如果能为我找到解决方案,我将不胜感激。

<a href="#" id="click-to-call">Click to call</a>
<script type="text/javascript">
var trackPixel = function() {
  var __autoFirePixels = true;
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.setAttribute('async', 'true');
  script.src = document.location.protocol+
  '//pixel.traffiliate.com/pixel/pixels.js.php'+
  'allsortsofapiinformation';
  document.documentElement.firstChild.appendChild(script);
}
document.getElementById('click-to-call').onclick = trackPixel;
document.getElementById('click-to-call2').onclick = trackPixel;
document.getElementById('click-to-call3').onclick = trackPixel;
</script>