jquery中的条件语句?

时间:2013-05-22 17:54:59

标签: jquery html5

我在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中使用条件语句的特殊方法?

2 个答案:

答案 0 :(得分:1)

你忘了关闭括号。

  else if(degree>(-33)&&degree<(-25){ is not correct
  else if(degree>(-33)&&degree<(-25)){ is correct

答案 1 :(得分:0)

应该像这样做

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