简体   繁体   English

跟踪页面上的所有元素

[英]Track all elements on page

I'm attempting to track events for all UI elements on a page. 我正在尝试跟踪页面上所有UI元素的事件。 The page contains dynamically generated content and various frameworks / libraries. 该页面包含动态生成的内容和各种框架/库。 Initially I tracked elements through creating a css class "track" , then adding style "track" to tracked elements. 最初,我通过创建css类“ track”来跟踪元素,然后将样式“ track”添加到跟踪的元素。 elements are then tracked using : 然后使用以下元素跟踪元素:

  $('.track').on('click', function() {
    console.log('Div clicked' + this.id);
    console.log(window.location.href);
    console.log(new Date().getTime());
  });

As content can be dynamically generated I wanted a method to track these elements also. 由于可以动态生成内容,因此我也需要一种跟踪这些元素的方法。 So tried this using wildcard jQuery operator. 因此,使用通配jQuery运算符尝试了此操作。

In this fiddle : https://jsfiddle.net/xx68trhg/37/ I'm attempting to track all elements using the jquery '*' selector. 在这个小提琴中: https : //jsfiddle.net/xx68trhg/37/我正在尝试使用jquery'*'选择器跟踪所有元素。

Using jQuery '*' selector appears to fire the event for all elements of given type. 使用jQuery'*'选择器似乎会触发给定类型的所有元素的事件。 So for this case if is clicked all the click event is fired for all divs. 因此,对于这种情况,如果单击,则会为所有div触发所有click事件。 But id is just available for div being clicked. 但是id只能用于被单击的div。

For the th element the click event is fired twice , what is reason for this ? 对于th元素,单击事件被触发两次,这是什么原因?

Can the source be modified that event is fired for just currently selected event ? 是否可以修改仅针对当前选定事件触发事件的源?

fiddle src : 小提琴src:

 $(document).ready(function() { $('*').each(function(i, ele) { $(this).addClass("tracked"); }); $('.tracked').on('click', function() { console.log('Div clicked' + this.id); console.log(window.location.href); console.log(new Date().getTime()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <!-- <div id="1" data-track="thisdiv"> Any clicks in here should be tracked </div> --> <div id="1"> Any clicks in here should be tracked 1 </div> <div id="2"> Any clicks in here should be tracked 2 </div> <div id="3"> Any clicks in here should be tracked 3 </div> <th id="th">tester</th> 

You can try with: 您可以尝试:

$(document).ready(function() {
    $("body > *").click(function(event) {
      console.log(event.target.id);
    });
});

 $(document).ready(function() { $("body > *").click(function(event) { console.log(event.target.id); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="1"> Any clicks in here should be tracked 1 </div> <div id="2"> Any clicks in here should be tracked 2 </div> <div id="3"> Any clicks in here should be tracked 3 </div> <table> <tr> <td>Cols 1</td> <td id="td">Cols 2</td> </tr> </table> <p id="th">tester</p> 

You may want to use event delegation to target the elements you need. 您可能想使用事件委托来定位所需的元素。 Advantage is that this also works for dynamically generated elements. 优点是,这也适用于动态生成的元素。 See code for an example of this. 有关示例,请参见代码。

 // method to add/set data-attribute and value const nClicksInit = (element, n = "0") => element.setAttribute("data-nclicked", n); // add data-attribute to all current divs (see css for usage) // btw: we can't use the method directly (forEach(nClicksInit)) // because that would send the forEach iterator as the value of parameter n document.querySelectorAll("div").forEach(elem => nClicksInit(elem)); // add a click handler to the document body. You only need one handler method // (clickHandling) to handle all click events document.querySelector('body').addEventListener('click', clickHandling); function clickHandling(evt) { // evt.target is the element the event is generated // from. Now, let's detect what was clicked. If none of the // conditions hereafter are met, this method does nothing. const from = evt.target; if (/^div$/i.test(from.nodeName)) { // aha, it's a div, let's increment the number of detected // clicks in data-attribute nClicksInit(from, +from.getAttribute("data-nclicked") + 1); } if (from.id === "addDiv") { // allright, it's button#addDiv, so add a div element let newElement = document.createElement("div"); newElement.innerHTML = "My clicks are also tracked ;)"; const otherDivs = document.querySelectorAll("div"); otherDivs[otherDivs.length-1].after(newElement); nClicksInit(newElement); } } 
 body { font: 12px/15px normal verdana, arial; margin: 2em; } div { cursor:pointer; } div:hover { color: red; } div:hover:before { content: '['attr(data-nclicked)' click(s) detected] '; color: green; } #addDiv:hover:after { content: " and see what happens"; } 
 <div id="1"> Click me and see if clicks are tracked </div> <div id="2"> Click me and see if clicks are tracked </div> <div id="3"> Click me and see if clicks are tracked </div> <p> <button id="addDiv">Add a div</button> </p> <h3 id="th">No events are tracked here, so clicking doesn't do anything</h3> 

You can invoke the stopPropagation and the condition this === e.currentTarget to ensure invoke the handler function of the event source DOM. 您可以调用stopPropagation和条件this === e.currentTarget以确保调用事件源DOM的处理函数。

And you must know the <th> tag must wrapped by <table> , otherwise it will not be rendered. 并且您必须知道<th>标记必须由<table>包装,否则它将不会呈现。

 $(document).ready(function() { $('*').each(function(i, ele) { $(this).addClass("tracked"); }); $('.tracked').on('click', function(e) { if (this === e.currentTarget) { e.stopPropagation(); console.log('Div clicked' + this.id); console.log(window.location.href); console.log(new Date().getTime()); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <!-- <div id="1" data-track="thisdiv"> Any clicks in here should be tracked </div> --> <div id="1"> Any clicks in here should be tracked 1 </div> <div id="2"> Any clicks in here should be tracked 2 </div> <div id="3"> Any clicks in here should be tracked 3 </div> <table> <th id="th">tester</th> </table> 

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

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