2016-12-07 87 views
-1

我尝试优化此代码:优化jQuery脚本

 $(".carre").each(function(){ 

    if ($(this).attr('taille') == '3-1') { 
     $(this).height(a*3).width(b*1); 
     $("a", this).height(a*3).width(b*1); 
    } 

    if ($(this).attr('taille') == '1-3') { 
     $(this).height(a*1).width(b*3); 
     $("a", this).height(a*1).width(b*3); 
    } 

    if ($(this).attr('taille') == '2-1') { 
     $(this).height(a*2).width(b*1); 
     $("a", this).height(a*2).width(b*1); 
    } 

    if ($(this).attr('taille') == '2-3') { 
     $(this).height(a*2).width(b*3); 
     $("a", this).height(a*2).width(b*3); 
    } 
}); 
} 

我尝试这样做:

 $(".carre").each(function(){ 

    var str = $(this).attr('taille'); 
    var h = str.charAt(0); 
    var w = str.charAt(str.length-1); 
     $(this).height(a*h).width(b*w); 
     $("a", this).height(a*h).width(b*w);}); 

但对于所有不工作,只为一类。

你能帮我请,

问候

+0

控制台中是否有任何错误?哪里定义了“a”和“b”? – billyonecan

回答

0

我没有安慰错误

我不能发布的所有代码。 这是a和b的值。

var a = $(".height-max").height()+1, 
b = $(".height-max").width()+1; 
0

您可以使用split字符串提取值:

var values = "3-1".split("_"); 

然后你就可以[0]和值[1]在你的计算访问值。