如何定义状态jquery / javascript?

时间:2018-10-30 12:44:08

标签: javascript jquery if-statement state

 $(document).on("click", '.gifyImage', function(event) {
    var clickedImage = $(event.target);
    if(clickedImage.data('state') === "animate") {
        //set to still
        clickedImage.data('state', "still");
        // update state clickedImage.data('state', 'animate');
        clickedImage.data('state', "animate");
    } else {
        //set to animate
        clickedImage.data('state', "animate");
        //update state to still
        clickedImage.data('state', "still");
    }
    clickedImage.attr('src', clickedImage.data('still'));
    console.log(event.target);
})

gifImage.data(state, 'animate');

^^^那是我打开开发工具时遇到的所谓错误

我为什么要编写此函数的背景知识。我有一项作业,要求我保持gif图像静止不动,并且单击gif时它将开始播放(动画),并且当我再次单击它时,它将回到静止。这段代码给我一个错误:“状态未定义” the error im getting

1 个答案:

答案 0 :(得分:3)

在这一行

        gifImage.data(state, 'animate');

state放入引号:

        gifImage.data('state', 'animate');