简单的jquery图像更改onclick失败

ian*_*ian 1 jquery

我有一个失败的简单jQuery图像开关.

$('.heart_img').click(function()
    {
        var heart_type = $(this).attr('src');

        //alert( heart_type );

        if ( heart_type == 'images/unheart.png')
        {
            //alert('uheart');
            $(this).attr('src','images/heart.png');
        }
        else if ( heart_type == 'images/heart.png');
        {
            $(this).attr('src','images/unheart.png');
        }

    });
Run Code Online (Sandbox Code Playgroud)

未注释时警报会正确触发,并且图像位于正确的位置,因此我不确定问题是什么.

Pet*_*tai 6

问题:

你的if分号

else if ( heart_type == 'images/heart.png');
Run Code Online (Sandbox Code Playgroud)

应该

else if ( heart_type == 'images/heart.png')
Run Code Online (Sandbox Code Playgroud)

甚至更好

else
Run Code Online (Sandbox Code Playgroud)