更改 JS 代码以使用 GTM(箭头函数和扩展表达式)

Change JS code in order to work with GTM (arrow function and spread expression)

我使用此 JS 代码从一系列 li 中的 a 元素获取标题值:

var res = [...document.querySelectorAll(".breadcrumb li:not(:first-of-type) > a")].map(el => el.getAttribute("title")).join(" - ");
alert(res)

我必须在 Google 跟踪代码管理器中使用它,但它不起作用,因为 GTM 不支持 ECMASCRIPT6。

如何更改箭头函数和扩展表达式以使其与标签管理器一起使用?

您可以在 NodeList 上调用 Array.prototype.slice 以转换为数组。箭头函数可以用普通函数代替:

var res = []
  .slice
  .call(document.querySelectorAll(".breadcrumb li:not(:first-of-type) > a"))
  .map(function (el) {
    return el.getAttribute('title')
  })
  .join(' - ')