简体   繁体   English

使用 JavaScript 跟踪所有点击的元素

[英]Track ALL clicked elements using JavaScript

I want to track ALL elements that are clicked on a HTML-page.我想跟踪在 HTML 页面上单击的所有元素。 I need a good way to reference exactly which element was clicked (so I will be able to replay the clicks on a identical separate HTML-page later on).我需要一种很好的方法来准确引用被点击的元素(以便我稍后能够在相同的单独 HTML 页面上重播点击)。

Is there a good way to reference which element that was clicked?有没有一种很好的方法来引用被点击的元素?

I could add unique id's and classnames to every single element on the page.我可以为页面上的每个元素添加唯一的 id 和类名。 But I figure there must be another way?但我想一定有另一种方式吗?

The HTML-page which I will be replaying the clicks on will be identical.我将重放点击的 HTML 页面将是相同的。

Something like this (but more exact information which element it was, maybe that's possible to collect)...像这样的东西(但更准确的信息是哪个元素,也许可以收集)......

Code to track which element was clicked跟踪点击了哪个元素的代码

var arrayWithElements = new Array();

document.onclick = clickListener;

function clickListener(e) {
    var clickedElement;
    if(e == null) {
        clickedElement = event.srcElement;
    } else {
        clickedElement = e.target;
    }
    arrayWithElements.push(clickedElement)
    alert(arrayWithElements);
}

Code that will be used on a identical HTML-page将在相同的 HTML 页面上使用的代码

document.someHowGetTheElement().onclick();

I guess you are looking for something like this:我猜你正在寻找这样的东西:


var arrayWithElements = new Array();

function clickListener(e) 
{   
    var clickedElement=(window.event)
                        ? window.event.srcElement
                        : e.target,
        tags=document.getElementsByTagName(clickedElement.tagName);

    for(var i=0;i<tags.length;++i)
    {
      if(tags[i]==clickedElement)
      {
        arrayWithElements.push({tag:clickedElement.tagName,index:i}); 
        console.log(arrayWithElements);
      }    
    }
}

document.onclick = clickListener;

It will store on every click an object that contains the tagName of the element and the index.它会在每次点击时存储一个包含元素的 tagName 和索引的对象。 So you may access this element in another "instance" of this document by using因此,您可以通过使用在本文档的另一个“实例”中访问此元素

document.getElementsByTagName(item.tag)[item.index]

(where item is an item of arrayWithElements) (其中item是 arrayWithElements 的一个项目)

Demo: http://jsfiddle.net/doktormolle/z2wds/演示: http : //jsfiddle.net/doktormolle/z2wds/

You can use this related question .您可以使用此相关问题 In other words, a good way to know which element was clicked without adding IDs all over the place is to use jquery's .cssSelectorAsString() method.换句话说,在不到处添加 ID 的情况下知道哪个元素被点击的好方法是使用 jquery 的.cssSelectorAsString()方法。 For example:例如:

function clickListener(e) {
    var clickedElement;
    if(e == null) {
        clickedElement = event.srcElement;
    } else {
        clickedElement = e.target;
    }
    arrayWithElements.push($(clickedElement).cssSelectorAsString()); // <====
    alert(arrayWithElements);
}    

See also: Get unique selector of element in Jquery另请参阅: 在 Jquery 中获取元素的唯一选择器

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

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