单击一个元素两次后执行操作(Execute action after an element is clicked twice)

编程入门 行业动态 更新时间:2024-10-28 05:13:57
单击一个元素两次后执行操作(Execute action after an element is clicked twice)

对于学校,我们需要做一个记忆游戏。 我们已经知道我们有卡片,你可以对它们进行分类。 但现在我们需要检查是否单击了2个元素,然后执行操作。 所有div都有一个唯一的Id和同一个类。 我知道你可以用.click做一个元素,但我想知道你是否可以为2个元素做这样的事情。

提前致谢。

For school we need to make a memory game. We're at the point that we have the cards and you can sort them. But now we need to check if 2 elements are clicked, and then execute an action. All the div's have an unique Id and a same class. I know you can do it for one element with .click, but I was wondering whether you can do something like that for 2 elements.

Thanks in advance.

最满意答案

如果每次单击一张卡时,表示已单击该卡,然后计算具有该指示符的卡的数量,则可以确定是否运行其他逻辑。 假设你的每张卡都有一个类“卡”来表示元素是卡片,代码看起来像这样:

$('.card').click(function(e) { $(this).addClass('clicked'); // indicate that this card has been clicked // any other logic run every time a card is clicked, such as turning it face up if($('.card.clicked').length === 2) { // query all .card.clicked elements, and compare the length (number of matched elements) to 2 // run logic if two cards have been clicked $('.card.clicked').removeClass('clicked'); // clear the "clicked" indicators } });

If, every time a card is clicked, you indicate that it has been clicked, and then count the number of cards that have that indicator, you can determine whether or not to run the additional logic. Assuming your cards each have a class "card" to indicate that the elements are cards, the code would look something like this:

$('.card').click(function(e) { $(this).addClass('clicked'); // indicate that this card has been clicked // any other logic run every time a card is clicked, such as turning it face up if($('.card.clicked').length === 2) { // query all .card.clicked elements, and compare the length (number of matched elements) to 2 // run logic if two cards have been clicked $('.card.clicked').removeClass('clicked'); // clear the "clicked" indicators } });

更多推荐

本文发布于:2023-07-23 22:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1238190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   单击   元素   操作   element

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!