简体   繁体   English

添加onclick()链接到Drupal 7

[英]Adding onclick() to link in drupal 7

I'm working in adding an onclick attribute to a few links on my drupal 7 site. 我正在为我的drupal 7网站上的几个链接添加onclick属性。 I added the menu_attributes module to assign an id to my links, tried adding the following code to a new js file I included in sites/themes/themeName.info . 我添加了menu_attributes模块来为链接分配一个ID,并尝试将以下代码添加到我包含在sites/themes/themeName.info的新js文件中。

function() {
  var link = document.getElementById('conversion');
  //link.setAttribute("target", "_blank");
  link.addEventListener("click", function(e){
        return gtag_report_conversion('http://www.mysite/page’); }); }

It's not adding onclick, would greatly appreciate any help. 它没有添加onclick,将不胜感激任何帮助。

Check whether the script is loading in the page. 检查脚本是否正在页面中加载。 See this for the details of adding javascript in drupal 7. 对在Drupal 7添加JavaScript的细节。

Based on JavaScript that executes after page load and adjusted for this specific problem this code works if placed inside head 基于JavaScript,该JavaScript在页面加载后执行,并针对此特定问题进行了调整,如果将其放在头部,则此代码有效

function addClick(){
                    alert('click'); //confirm it works
                    return gtag_report_conversion('http://mySite/myPage');
                }

                 //make sure this runs after dom is loaded.. so element can be found
                 document.addEventListener("DOMContentLoaded", function(){
                    var link = document.getElementById("cteconversion");
                    link.onclick=addClick;
                 });

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

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