写一个对象(类)里面有一个isyear属性  两个方法setyear() leapyear()
leapyear()用来判断输入的数是不是闰年
setyear() 如果没有输入自动生成一个1000——2011的随机数然后判断是否是闰年body中
有一个text文本框 (输入年份)
一个button用于判断

解决方案 »

  1.   


    <script>
    function Year(){
        this.isyear = null;
        this.setyear = function(y){
         if(y=="" || isNaN(y)){
         y = parseInt(Math.random()*(2011-1000+1)+1000);
         }
         this.isyear = y;
        };
        this.leapyear = function(){
         var b = (this.isyear%4==0 && this.isyear%100!=0)||(this.isyear%100==0 && this.isyear%400==0);
         if(b) alert(this.isyear + "是闰年")
         else alert(this.isyear + "不是闰年")
        };
    }
    function doit(obj){
    var v = obj.value;
    var y = new Year();
    y.setyear(v);
    y.leapyear();
    }
    </script>
    <input type="text" id="txt"><input type="button" value="click" onclick="doit(document.getElementById('txt'));">
      

  2.   

    <!DOCTYPE html>
    <html>
    <head>
        <title>test</title>
        <script src="jquery.js"></script>
    </head>
    <body>
    <form>
        <input id="testinput" type="text" value="" />
        <input id="testbtn" type="button" value="check" />
    </form>
        <script>
         $('#testinput').keypress(function(event){
         var which = event.which;
         if((which < 48 || which > 57) && which !== 0 && which !== 8){
         return false;
         }
         });
         $('#testbtn').click(function(){
         var val = $('#testinput').val(),
         yearObj = new YesrCheck();
         yearObj.setYear(val);
         alert(yearObj.isYear());
         });
         function YesrCheck(){
         var year;
        
         this.isYear = function(){
         var ret;
         if(year % 4 !== 0){
         ret = false;
         }else if(year % 100 !== 0){
         ret = true;
         }else if(year % 400 === 0){
         ret = true;
         }else{
         ret = false;
         }
         return ret;
         };
        
         this.setYear = function(val){
         if(val !== '' && val.length <= 4){
         year = val;
         }else{
         year = randomYear();
         }
         }
         function randomYear(){
         var rand = Math.ceil(Math.random() * 2011);
         if(rand < 1000){
         rand = randomYear();
         }
         return rand;
         }
         }
        </script>
    </body>
    </html>
      

  3.   

    <!DOCTYPE html>
    <html>
    <head>
        <title>test</title>
        <script src="jquery.js"></script>
    </head>
    <body>
    <form>
        <input id="testinput" type="text" value="" />
        <input id="testbtn" type="button" value="check" />
    </form>
        <script>
         $('#testinput').keypress(function(event){
         var which = event.which;
         if((which < 48 || which > 57) && which !== 0 && which !== 8){
         return false;
         }
         });
         $('#testbtn').click(function(){
         var val = $('#testinput').val(),
         yearObj = new YesrCheck();
         yearObj.setYear(val);
         alert(yearObj.isLeapYear());
         });
         function YesrCheck(){
         var year;
         this.isyear = false;
         this.isLeapYear = function(){
         var ret;
         if(year % 4 !== 0 || (year % 100 === 0 && year % 400 !== 0)){
         ret = false;
         }else{
         ret = true;
         }
         return ret;
         };
        
         this.setYear = function(val){
         if(val !== '' && val.length <= 4){
         this.isyear = true;
         year = val;
         }else{
         this.isyear = false;
         year = randomYear();
         }
         }
         function randomYear(){
         var rand = Math.ceil(Math.random() * 2011);
         if(rand < 1000){
         rand = randomYear();
         }
         return rand;
         }
         }
        </script>
    </body>
    </html>
      

  4.   

    <!DOCTYPE html>
    <html>
    <head>
        <title>test</title>
        <script src="jquery.js"></script>
    </head>
    <body>
    <form>
        <input id="testinput" type="text" value="" />
        <input id="testbtn" type="button" value="check" />
    </form>
        <script>
         $('#testinput').keypress(function(event){
         var which = event.which;
         if((which < 48 || which > 57) && which !== 0 && which !== 8){
         return false;
         }
         });
         $('#testbtn').click(function(){
         var val = $('#testinput').val(),
         yearObj = new YesrCheck();
         yearObj.setYear(val);
         alert(yearObj.isLeapYear());
         });
         function YesrCheck(){
         var year;
         this.isyear = false;
        
         this.isLeapYear = function(){
         var ret;
         if(year % 4 !== 0 || (year % 100 === 0 && year % 400 !== 0)){
         ret = false;
         }else{
         ret = true;
         }
         return ret;
         };
        
         this.setYear = function(val){
         if(val !== '' && val.length <= 4){
         this.isyear = true;
         year = val;
         }else{
         this.isyear = false;
         year = Math.floor(Math.random() * 1012) + 1000;
         }
         }
         }
        </script>
    </body>
    </html>
      

  5.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>判断是否是闰年</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>    <script type="text/javascript">
            function Year()
            {
                this.isyear=null;
                this.setyear=function(y){
                    if(y=="" || isNaN(y)){
                        y = parseInt(Math.random()*(2011-1000+1)+1000);
                    }
                    this.isyear=y;
                    return this.leapyear(y);
                };
                this.leapyear = function(){
                   var b = (this.isyear%4==0 && this.isyear%100!=0)||(this.isyear%100==0 && this.isyear%400==0);
                   if(b) return this.isyear + "是闰年";
                   else return this.isyear + "不是闰年";
                };
            }
            
            $(document).ready(function(){
                $("#but1").click(function(){
                    var txt = $.trim($("#txt1").val());
                    var a = new Year();
                    alert(a.setyear(txt));
                });
            });
            
        </script>
    </head>
    <body>
        <input type="text" id="txt1" value="" />
        <input type="button" id="but1" value="点击" />
    </body>
    </html>
      

  6.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>判断是否是闰年</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        
        <script type="text/javascript">
           
           /*
            写一个对象(类)里面有一个isyear属性 两个方法setyear() leapyear()
            leapyear()用来判断输入的数是不是闰年
            setyear() 如果没有输入自动生成一个1000——2011的随机数然后判断是否是闰年        body中
            有一个text文本框 (输入年份)
            一个button用于判断
           */
            function Year()//写一个对象(类)里面有一个isyear属性 两个方法setyear() leapyear()
            {
                this.isyear=null;
                this.setyear=function(y){//setyear()如果没有输入自动生成一个1000——2011的随机数然后判断是否是闰年
                    if(y=="" || isNaN(y)){
                        y = parseInt(Math.random()*(2011-1000+1)+1000);
                    }
                    this.isyear=y;
                    return this.leapyear(y);//然后判断是否是闰年
                };
                this.leapyear = function(){//leapyear()用来判断输入的数是不是闰年
                   var b = (this.isyear%4==0 && this.isyear%100!=0)||(this.isyear%100==0 && this.isyear%400==0);
                   if(b) return this.isyear + "是闰年";
                   else return this.isyear + "不是闰年";
                };
            }
            
            $(document).ready(function(){
                $("#but1").click(function(){
                    var txt = $.trim($("#txt1").val());
                    var a = new Year();
                    alert(a.setyear(txt));
                });
            });
            
        </script>
    </head>
    <body>
        <input type="text" id="txt1" value="" /><!--有一个text文本框 (输入年份)-->
        <input type="button" id="but1" value="点击" /><!--一个button用于判断-->
    </body>
    </html>