【问题标题】:Trying to implement if statement using hasClass(), but it's not working尝试使用 hasClass() 实现 if 语句,但它不起作用
【发布时间】:2013-04-29 15:56:32
【问题描述】:

我正在做一个记忆游戏。当一张卡片被翻转时,类 .flip 被添加到该卡片中。我使用 hasClass() 方法检查是否已将 .flip 类添加到两者中,从而跟踪是否选择了两张相同的卡片。

但是,用于 hasClass() 的 jQuery 似乎不起作用。我正在使用控制台日志进行检查,但控制台没有打印任何内容。这是我的 jQuery 代码:

$(document).ready(function(){

        var counter = 0;

        if(counter == 0){
            console.log(counter);
            // set up click/tap panels
            $('.click').toggle(function(){
                counter = 1;
                console.log(counter);
                $(this).addClass('flip');
            },function(){
                /*$(this).removeClass('flip');*/
            });
        }

        if($("#heart-01").hasClass("flip") && $("#heart-02").hasClass("flip")){
            console.log("yo");
        }

    });

这是 HTML:

<div id="heart-01" class="panel click heart">

    <div class="front"></div>

    <div class="back"></div>

</div>

<div id="heart-02" class="panel click heart">

    <div class="front"></div>

    <div class="back"></div>

</div>

还有 CSS:

.panel {
        float: left;
        width: 150px;
        height: 150px;
        margin: 20px;
        position: relative;
        -webkit-perspective: 600px;
        -moz-perspective: 600px;
    }

    /* -- Y axis rotation and general style for heart card -- */

    .heart .front {
        float: none;
        position: absolute;
        top: 0;
        left: 0;
        z-index: 900;
        width: inherit;
        height: inherit;
        border: 0;
        background: #333;
        text-align: center;

        -moz-box-shadow: 0 1px 5px rgba(0,0,0,0.9);
        -webkit-box-shadow: 0 1px 5px rgba(0,0,0,0.9);
        box-shadow: 0 1px 5px rgba(0,0,0,0.9);

        -webkit-transform: rotateX(0deg) rotateY(0deg);
        -webkit-transform-style: preserve-3d;
        -webkit-backface-visibility: hidden;

        -moz-transform: rotateX(0deg) rotateY(0deg);
        -moz-transform-style: preserve-3d;
        -moz-backface-visibility: hidden;

        -o-transition: all .4s ease-in-out;
        -ms-transition: all .4s ease-in-out;
        -moz-transition: all .4s ease-in-out;
        -webkit-transition: all .4s ease-in-out;
        transition: all .4s ease-in-out;
    }
    .heart.flip .front {
        z-index: 900;
        background: #333;

        -webkit-transform: rotateY(180deg);
        -moz-transform: rotateY(180deg);

        -moz-box-shadow: 0 15px 50px rgba(0,0,0,0.2);
        -webkit-box-shadow: 0 15px 50px rgba(0,0,0,0.2);
        box-shadow: 0 15px 50px rgba(0,0,0,0.2);
    }

    .heart .back {
        float: none;
        position: absolute;
        top: 0;
        left: 0;
        z-index: 800;
        width: inherit;
        height: inherit;
        border: 0;
        background: url('images/card-01.png') 0 0 no-repeat;
        text-shadow: 1px  1px 1px rgba(0,0,0,0.6); 

        -webkit-transform: rotateY(-180deg);
        -webkit-transform-style: preserve-3d;
        -webkit-backface-visibility: hidden;

        -moz-transform: rotateY(-180deg);
        -moz-transform-style: preserve-3d;
        -moz-backface-visibility: hidden;

        -o-transition: all .4s ease-in-out;
        -ms-transition: all .4s ease-in-out;
        -moz-transition: all .4s ease-in-out;
        -webkit-transition: all .4s ease-in-out;
        transition: all .4s ease-in-out;
    }

    .heart.flip .back {
        z-index: 1000;
        background: url('images/card-01.png') 0 0 no-repeat;

        -webkit-transform: rotateX(0deg) rotateY(0deg);
        -moz-transform: rotateX(0deg) rotateY(0deg);

        box-shadow: 0 15px 50px rgba(0,0,0,0.2);
        -moz-box-shadow: 0 15px 50px rgba(0,0,0,0.2);
        -webkit-box-shadow: 0 15px 50px rgba(0,0,0,0.2);
    }
.click .front {
        cursor: pointer;
        -webkit-transform: rotateX(0deg);
        -moz-transform: rotateX(0deg);
    }
    .click.flip .front {
        -webkit-transform: rotateX(180deg);
        -moz-transform: rotateX(180deg);
    }
    .click .back {
        cursor: pointer;
        -webkit-transform: rotateX(-180deg);
        -moz-transform: rotateX(-180deg);
    }
    .click.flip .back {
        -webkit-transform: rotateX(0deg);
        -moz-transform: rotateX(0deg);
    }

【问题讨论】:

  • 请注意,您对 .toggle() 的使用一直是deprecated in 1.8 and removed in 1.9
  • the jQuery for hasClass() doesn't seem to be working -- 你做了什么来验证这一点?您是否曾在 console.log 中看到值 > 0 的 counter
  • @DavidTansey 他怎么可能?该函数只调用一次,并立即将值设置为 0。
  • 我的观点完全正确——如果他在执行addClass() 调用的代码行中找到了 value = 1...
  • @DavidTansey 他可以进行addClass 调用,但hasClass 测试不在事件处理程序中,它们在页面加载时调用,在addClass 调用之前曾经触发过。

标签: jquery css class


【解决方案1】:

您需要将测试类的if 子句放入 事件处理程序中。目前它是document.ready 处理程序的一部分,因此测试只在页面首次加载时发生一次。

另外,.toggle() 的两个功能版本已被弃用。

试试这个,它似乎具有您需要的全部功能:

$(document).ready(function(){

    $('.click').on('click', function() {
        $(this).toggleClass('flip');
        if ($('.flip').length === 2) {
            console.log('yo');
        }
    });

});

http://jsfiddle.net/alnitak/Xtw58/

【讨论】:

  • 所以我需要创建一个事件处理程序,仅在点击卡片时调用,而不是在加载页面时调用?这就是你写$('.click').on('click', function()时正在做的事情吗?
  • 另外,我需要检查两张卡是否都有“翻转”类。我如何使用 hasClass 做到这一点?我不能只使用计数器,因为记忆游戏将有另外八张牌。计数器只是用来确定第一张牌何时被翻转。我希望这是有道理的。
  • @ArielCotton 首先,您必须定义“两张卡”的含义,也就是说,您必须弄清楚要测试哪些卡。上面的代码只检查有两张卡的类为flip,所以下一步可以(在if内)检查这两张.flip卡是否有其他一些共同的属性。
猜你喜欢
  • 2021-05-27
  • 2023-03-23
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多