1

I'm working in a card game system that the player can select the card by clicking on it and then select the place to put it on. My problem is that when the player click on the target place, nothing happens.
This is my try: http://jsfiddle.net/5qMHz/
And this is my code:

function target() {
 $(".target").on("click", function() {
     $("#"+x).appendTo(this);
     console.log(x);     
 });
};

What's wrong?

4

3 に答える 3

2

まず、関数参照を jQuery オブジェクトに入れるというあなたのやり方はかなり奇妙です。ただし問題は.target、DOM のロード後にクラスが適用されるため、デリゲート セレクターを使用する必要があることです。これを試して:

var $card

$(".card").on("click", function () {
    $card = $(this);

    if ($(".myslot").length) {
        if ($(".myslot").is(':empty')) {
            $(".myslot:empty").addClass("target");
        } else {
            alert('No empty slots');
        }
    }
});

$('.field').on('click', ".target", function () {
    $card.appendTo(this);
    $card = $();
});

フィドルの例

于 2013-11-08T15:14:21.400 に答える