简体   繁体   English

追加点击事件

[英]on click event in append

I'm trying to add on click event in a parsexml append but having issues getting it done correctly keeps getting console errors. 我试图在parsexml追加中添加click事件,但是在正确完成操作时遇到问题会导致控制台错误。 Maybe I'm doing something wrong in the code but not sure how I should do it. 也许我在代码中做错了事,但不确定应该怎么做。 Basically I'm trying to get click events in google analytics using datalayer. 基本上,我正在尝试使用数据层在Google Analytics(分析)中获取点击事件。 this is the code 这是代码

function parseXml(xml)
 {
    $("#main").html("<div class='section group' id='content' data- 
     role='listview' data-inset='true'></div>");
     $(xml).find("item").slice(0, 2).each(function()
    {
    $("#content").append("<a onclick="
      dataLayer.push({
     'event': 'myTrackEvent',
     'eventCategory': 'clicked',
     'eventAction': 'click',
     'eventLabel': 'article click'
     });"class='articleClick' target='_blank' 
     href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'> 
   <div class='thumbnail'><img 
src='"+$(this).find("enclosure").attr('url') 
+"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+" 
   </h2><p>"+$(this).find("description").text()+"</p></div></div> 
   </a>");
      });  

    }

Your code is invalid here: 您的代码在这里无效:

$("#content").append("<a onclick="
  dataLayer.push({

.append() expects you to finish the string .append()希望您完成字符串

$("#content").append("<a onclick="  dataLayer.push({..    // that is what javascript sees

what you need to do is append the string: 您需要做的是附加字符串:

$("#content").append("<a onclick=" +
  dataLayer.push({

or 要么

$("#content").append("<a onclick=")
             .append(dataLayer.push({...}));

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

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