JQuery中toggle被淘汰后的替代方法

      今天使用toggle()方法的多函数切换功能来实现时间控件的开关切换效果,在浏览器浏览效果正常,在手机端页面就无法加载出来,估计是toggle()在手机端无法支持,使用以下方式解决。

        使用toggle():        // 为输入框添加click事件
        $('#' + this.inputId).toggle(
          function(){
    __method.init();
       __method.show(false, __method);
       return false;
          },
            function(){
        __method.close();
        return false;
            },
        );


使用替代方法后: // 为输入框添加click事件
    var clickNum = 0
    $('#' + this.inputId).click(function(){
     	if(clickNum == 0){
     		__method.init();
	        __method.show(false, __method);
	        clickNum = 1;
	        return false;
     	}else{
     		 __method.close();
     		 clickNum = 0;
        	return false;
     	};
    });

在最新的jQuery的程序库中jquery-2.2.3.js中已经有好几个函数被替换到了。应该说版本过1.8或1.9时就淘汰了。 

如:
  1. .live() 1.9以上被淘汰。 替代函数:.on()。
  2. .die() 1.9以上被淘汰。 替代函数:.off()。
  3. .size() 1.8以上被淘汰。替代函数:.length。
  4. .toggle() 1.8以上被淘汰。

    对于toggle,一般都会用if进行替换。 
    如正常用toggle:

$(".one .top").toggle(
        function (){
             $(".content").show(1500);
             $(".iocn").addClass("jian");

        },
        function (){
             $(".content").hide("slow");
               $(".iocn").addClass("jia");

        }
        );


替换方法一:

$(".one .top").click(function() {
            if($(".content").css("display")=="none"){
                 $(".content").show(1500);
                 $(".iocn").addClass("jian");
            }else {

                 $(".content").hide("slow");
                 $(".iocn").addClass("jia");
            }

        });


当然上面的替换方法有局限性。替换方法二:if语句。

var i=0;
        $(".one .top").click(function() {
            if(i==0){
                 $(".content").hide("slow");
                 $(".iocn").addClass("jia");
                 i=1;
            }else  {
                 $(".content").show(1500);
                 $(".iocn").addClass("jian");
                 i=0;
            }

        });

猜你喜欢

转载自blog.csdn.net/it_ping/article/details/80736924