简体   繁体   English

在 Div 中的多次点击事件返回为一次点击

[英]On Multiple click events within Div return as one click

I am in a bit of a bind.我有点为难。 I have multiple cards.. with several click events possible (7) to be exact inside of each card.我有多张卡片.. 可能有几个点击事件 (7) 确切地说在每张卡片内部。

I've added the same class to them all (each click event) with data tags that return different values.我已经将相同的 class 添加到它们(每个点击事件)中,数据标签返回不同的值。

Basically, I want to track all click events (7) at different times and check them as one per card.基本上,我想在不同时间跟踪所有点击事件 (7),并将它们作为每张卡片检查一次。

If the user does anything within the card at all, count it as one event.. I am basically trying to count the views per card.如果用户在卡片中做了任何事情,就把它算作一个事件。我基本上是在计算每张卡片的浏览量。

Right now I have something like this现在我有这样的东西

 $(document).on("click", ".test", function() {
    console.log('Testing Stats View capture');
    var that = $(this);
    var testStats = that.data('capture-stats');
    console.log(testStats);   
 }));

count views per card计算每张卡片的浏览量

You can store the count on each card using this.data() :您可以使用this.data()将计数存储在每张卡片上:

 $(document).on("click", ".card", function() { var clickcount = $(this).data("clickcount") || 0; clickcount++; $(this).data("clickcount", clickcount); console.log($(this).text() + " clicked: " + clickcount); });
 .card { cursor: pointer; user-select: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='card'>card 1</div> <div class='card'>card 2</div> <div class='card'>card 3</div>

You could add a data attribute to the element triggering the event, at the end of your handler, and check its presence at the beginning, as a condition to perform a given logic:您可以在处理程序的末尾向触发事件的元素添加数据属性,并在开始时检查它是否存在,作为执行给定逻辑的条件:

 $(document).on("click", ".test", function() { console.log('Testing Stats View capture'); var that = $(this); //if the prop data-event-triggered is not set yet for this element if (.that.data('event-triggered') ){ var testStats = that;data('capture-stats'). //adds the prop data-event-triggered for this element that,data('event-triggered';'true'). console;log('event triggered-${testStats}'); } });
 .test { width: 100px; height: 100px; background: gray; cursor: pointer; margin-bottom:10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="test" data-capture-stats="1">1</div> <div class="test" data-capture-stats="2">2</div>

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

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