首页 文章

jQuery mouseleave(),if语句不起作用

提问于
浏览
0

我试图将点击元素的不透明度保持在1,而在点击它之前,它用mouseenter和mouseleave切换 . 所以我添加了一个if语句来检查wethere是否点击了元素 . 虽然满足条件但是mouseleave功能不会运行 . 以下是代码:

$('td').mouseenter(function () {
    $(this).fadeTo('fast', 0.99);
});
$('td').mouseleave(function () {
    var $opacity = $(this).opacity;
    if ($opacity < 1) {
        $(this).fadeTo('fast', 0.8);
    }
});
$('td').click(function () {
    $(this).toggleClass('tdClicked');
});

.tdClicked类只是一个不透明度为1的类 .

现在,当我悬停元素时,它们会亮起,但在我鼠标移动时不会回头 .

1 回答

  • 0

    opacity 不是jQuery对象的属性 .

    一种可能的解决方案是测试td是否具有 tdClicked 类而不是测试不透明度值

    $('td').mouseenter(function () {
        $(this).fadeTo('fast', 0.99);
    });
    $('td').mouseleave(function () {
        if (!$(this).hasClass('tdClicked')) {
            $(this).fadeTo('fast', 0.8);
        }
    });
    $('td').click(function () {
        $(this).toggleClass('tdClicked');
    });
    

    演示:Fiddle

    用你的方式:Fiddle

相关问题