努力添加模糊事件监听器

Dou*_*Fir 3 javascript blur data-layers onblur google-tag-manager

我正在工作的网站上有一个搜索框,并希望跟踪人们正在寻找的搜索字词。

由于搜索框会自动猜测并输入用户正在搜索的内容,因此没有点击事件。

在控制台中,当发生模糊时,我想要的textContent是以下内容的.textContent:

myVar = document.querySelectorAll('.twitter-typeahead > span')[2]
Run Code Online (Sandbox Code Playgroud)

但是,仅当有人实际键入内容时,此处返回的值才不是null。因此,附加模糊事件似乎是可行的方法。在另一个论坛上某人的帮助下,我在控制台中看到了以下内容:

myVar.addEventListener('blur', function(){dataLayer.push({'event':'bla'})})
Run Code Online (Sandbox Code Playgroud)

在控制台中键入所有内容之后,我看不到任何值被推送到dataLayer,这使我认为blur事件不起作用(与dataLayer.push函数相对)。

有关搜索框的页面在此处

我如何将模糊事件附加到使搜索框失去焦点的人?

Bar*_*mar 5

您应该将blur侦听器添加到搜索框,而不是跨度:

searchbox = document.getElementById('searchboxID');
searchbox.addEventListener('blur', function() {
    var input = this.value;
    // Do whatever you want with the input
});
Run Code Online (Sandbox Code Playgroud)