-2

我在 jquery 中创建条件语句时遇到困难,原始代码如下所示:

http://jsfiddle.net/phann/7Qzzg/1/

我对代码进行了一些修改,如下所示。但它似乎不起作用。有没有人可以给我解决方案?

    <script type="text/javascript">
//using jquery for click event
$(document).ready(function(){
    var img = $('.image');
    var textStatus = "";
    var timeofclock;
    var randomnumber = 1 + Math.floor(Math.random()*12); //create random number from 1-12
    $("#randomnumber_value").html(randomnumber);
    if(img.length > 0){
        var offset = img.offset();
        function mouse(evt){
            var center_x = (offset.left) + (img.width()/2);
            var center_y = (offset.top) + (img.height()/2);
            var mouse_x = evt.pageX; var mouse_y = evt.pageY;
            var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
            var degree = (radians * (180 / Math.PI) * -1) + 90; 


            img.css('-moz-transform', 'rotate('+degree+'deg)');
            img.css('-webkit-transform', 'rotate('+degree+'deg)');
            img.css('-o-transform', 'rotate('+degree+'deg)');
            img.css('-ms-transform', 'rotate('+degree+'deg)');
            $("#degree_value").html(degree);

            if(degree>(-61)&&degree<(-55)){
                timeofclock = 1;
            }
            else if(degree>(-33)&&degree<(-25){
                timeofclock = 2;
            }
            else if(degree>(-1)&&degree<(2){
                timeofclock = 3;
            }

            //jika benar maka angka berubah, tampilkan status menang
            if(timeofclock==randomnumber){
                //randomnumber = 1 + Math.floor(Math.random()*12); //create random number from 1-12
                //$("#randomnumber_value").html(randomnumber);
                textStatus = "CORRECT..!";
                $("#textStatus").html(textStatus);

            }
            else{
                textStatus = "";
                $("#textStatus").html(textStatus);
            }
        }
        $(document).mousedown(mouse);
    }
});
</script>

从我使用的编辑器中,错误显示在以下行中else if(degree>(-33)&&degree<(-25){

有没有一种特殊的方法可以在 jquery 中使用条件语句?

4

2 回答 2

1

你忘了关闭括号。

  else if(degree>(-33)&&degree<(-25){ is not correct
  else if(degree>(-33)&&degree<(-25)){ is correct
于 2013-05-22T17:57:00.433 回答
0

应该这样做

if(degree > -61 && degree < -55){
            timeofclock = 1;
        }
        else if(degree > -33 && degree < -25){
            timeofclock = 2;
        }
        else if(degree > -1 && degree < 2){
            timeofclock = 3;
        }
于 2013-05-22T17:54:58.520 回答