我正在使用jQuery和PHP创建一个内存游戏(从这里找到的脚本中解脱出来),本质上我试图做的是,当两张牌匹配时,它们都被移除。但是,我正在使用两个不同的图像进行配对。在 HTML 表单中,每张卡片如下所示:
<div class="card {toggle:'imagename'}">
<div class="off"></div>
<div class="on {toggle:'imageid'}"></div>
</div>
本质上,当选择两张卡时,jQuery应该检查id是否匹配。最初应该检查图像是否匹配,但是作为两个不同的图像,我无法做到这一点(它只显示一个)。下面是jQuery:
function matchingCards(event, params){
$cards = $("."+params.css_class+".on:visible");
$.each($cards, function(index, card){
var $card = $(card);
$card.trigger("card_removed");
$card.parent(".card").unbind("*").before("<div class='card_ph'></div>").remove();
$card = null;
});
$cards_left = $("#game_board>.card");
if ( $cards_left.length == 0 ){
$(document).trigger("game_won", {});
/*
* quickFlip has a problem when working in IE: when the last
* element bound was removed the problem is caused by the bound
* resize event on the window and is causing
* the end game get stuck when the game is over...
*/
$(window).unbind("resize");
}
$cards_left = $cards = null;
};
function checkCards(){
$on_cards = $("#game_board .card>.on:visible");
if ( $on_cards.length == 2 ){
$(document).trigger("player_made_move");
// Get the first object css class
var css_class = $on_cards.parent(".card").metadata()["toggle"];
var $card = $(this);
var this_card = $card.children(".on").metadata()["toggle"];
$matched_cards = $on_cards.filter("."+this_card);
var event_name = "no_match";
if ( $matched_cards.length == 2 ){
event_name = "found_match";
}
if ( css_class == css_class ){
event_name = "no_match";
}
$(document).trigger(event_name, {css_class: css_class});
$matched_cards = null;
}
clearTimeout(chk_cards_timeout);
chk_cards_timeout = null;
$on_cards = null;
};
任何帮助将不胜感激。
附加信息:
这些对具有相同的图像名称,我刚刚在其中一张添加了"_desc"作为CSS类名(即一个图像标题为"i1",另一个标题为"i1_desc")
当发现一对卡片时,它们不会重置到原始位置或消失。
试试这个流程。
1.点击卡片时...将类更改为"打开"。当再次单击时,类将更改为默认的"隐藏"。
。每张卡都有一个 ID image_name+DESC ...因此,2张卡将具有相同的ID。
2.每次点击后,使用 jQuery each() 函数搜索"open"类并获取元素的 id。
3)比较这两个id,如果相似,则再次使用each()函数删除元素。如果您不使用 each() 函数,则只会删除 1'st 元素。
PS:别忘了数数,跟踪ID,得分..等。
。each() 函数在操作 http://api.jquery.com/jQuery.each/