首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GTM自定义Javascript变量不起作用(返回函数)

GTM自定义Javascript变量不起作用(返回函数)
EN

Stack Overflow用户
提问于 2021-04-23 10:41:18
回答 2查看 60关注 0票数 0

我已经看了好几天了,完全被卡住了。我正在使用的页面如下所示:

代码语言:javascript
复制
<div class="field--item">
<span class="file file--mime-application-pdf file--application-pdf icon-before">
<div class="file-info text-center--mobile"><span class="file-icon"><span class="icon glyphicon glyphicon-file text-primary" aria-hidden="true"></span></span><div class="file-wrapper"><span class="file-name">17840.pdf</span><span class="file-size">94.35 KB</span></div></div>
<span class="file-link"><a href="/dashboard/download/17840/122526" class="resource-button" resource-name="Kennedy's actions in Vietnam in 1962" resource-file-id="122526" resource-file-type="PDF" resource-subject="History" resource-category="">Download</a>
</span></span></div>

我设置的自定义变量如下所示:

代码语言:javascript
复制
function() {
    return document.getElementsByClassName('resource-button')[0].getAttribute('resource-name').text();
}

我真正想做的是设置一个变量,在单击Download按钮时拉入资源名称。我知道要单独设置实际的点击跟踪等,我就是不能让这个函数通过除'defined‘之外的任何变量。

在正确的方向上的任何帮助或点头都是非常感谢的。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-23 11:46:40

尝试删除末尾的text()函数:

代码语言:javascript
复制
function getAttribute() {
  return document.getElementsByClassName('resource-button')[0].getAttribute('resource-name');
}

console.log(getAttribute());
代码语言:javascript
复制
<div class="field--item">
<span class="file file--mime-application-pdf file--application-pdf icon-before">
<div class="file-info text-center--mobile"><span class="file-icon"><span class="icon glyphicon glyphicon-file text-primary" aria-hidden="true"></span></span><div class="file-wrapper"><span class="file-name">17840.pdf</span><span class="file-size">94.35 KB</span></div></div>
<span class="file-link"><a href="/dashboard/download/17840/122526" class="resource-button" resource-name="Kennedy's actions in Vietnam in 1962" resource-file-id="122526" resource-file-type="PDF" resource-subject="History" resource-category="">Download</a>
</span></span></div>

票数 0
EN

Stack Overflow用户

发布于 2021-04-23 10:49:11

也许你可以使用click事件:

代码语言:javascript
复制
<a href="/dashboard/download/17840/122526" onclick="myFunction(this);">Download</a>

<script>
    function myFunction(elem) {
       var yourAttribute = elem.getAttribute('yourAttribute');
    }
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67223279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档