![](/img/trans.png)
[英]How do i add an individual onclick/EventListener function to each row of PHP generated content?
[英]How do I add an onClick event to a link generated on PHP?
我正在尝试在PHP文件中的链接上添加onclick事件。
$body2 .= '<td class="sampling-list-td download-link">' . '<a ' . 'class="sampling-list-download" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
这就是我想要做的:
$body2 .= '<td class="sampling-list-download" ' . 'onClick="ga('send', 'event', { eventCategory: 'download', eventAction: 'click', eventLabel: 'Files Download'});" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
但是我收到以下错误:
ParseError:语法错误,意外的“发送”(T_STRING)
我不知道我在做什么错? 我正在尝试为Google Analytics(分析)T_T添加事件跟踪代码
'send'
的引号结束了您在onClick之前开始的字符串。 当字符串终止时,它将再次解释为PHP,并且send
在这里不是有效的关键字。
为防止这种情况,您可以转义'
例如: 'onClick="ga(\\'send\\', \\'event\\', { e...
它的报价问题.change喜欢这个添加\\
之前'
中onclick.Otherwise每次'
。是打破字符串和文本后的执行,如PHP语句或变量。如果PHP send
无效之一,所以你得到了错误
$body2 .= '<td class="sampling-list-download" ' . 'onClick="ga(\'send\', \'event\', { eventCategory: \'download\', eventAction: \'click\', eventLabel: \'Files Download\'});" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.