我是jQuery的新手,需要通过编写循环遍历3个独立变量的函数来削减一些代码行。下面是我试图描述这个(请注意,对象可以自行运行,我只是循环自定义参数的函数:TYPE,POSITION AND RANK):
//TYPE: e, br, ie
//POSITION: l, r
//RANK: 1, 2, 3, 4, 5
//my script starts here
var TYPEPOSITIONRANKname = "label";
var TYPEPOSITIONRANKvalue = "percentage";
var TYPEPOSITIONRANKimg = "imgurl";
$("#statsTYPE .numberRANKranked .infobox_POSITION h3 span").text(TYPEPOSITIONRANKname);
$("#TYPEPOSITIONRANK img").attr({
alt: TYPEPOSITIONRANKname,
src: TYPEPOSITIONRANKimg
});
var TYPEPOSITIONRANKinter = $("#statsTYPE .numberRANKranked .infobox_POSITION p").text().replace(/[^0-9]+/g, "");
var TYPEPOSITIONRANKheight = parseInt(TYPEPOSITIONRANKinter, 10) /10 + 5;
$("#TYPEPOSITIONRANK").height(TYPEPOSITIONRANKheight + "%");
所有CAPS中的单词都是我的“参数”。在下面的示例中,TYPE = e,POSITION = 1且RANK = 1:
var el1name = "label";
var el1value = "percentage";
var el1img = "imgurl";
$("#statse .number1ranked .infobox_l h3 span").text(el1name);
$("#el1 img").attr({
alt: el1name,
src: el1img
});
var el1inter = $("#statse .number1ranked .infobox_l p").text().replace(/[^0-9]+/g, "");
var el1height = parseInt(el1inter, 10) /10 + 5;
$("#el1").height(el1height + "%");