Javascript Jquery 中的数组值 [已修复]
Javascript array value in Jquery [fixed it]
那么,我有一个问题。该函数应该获取#content 数组第一个变量值的高度。
我尝试了很多不同的方法,但我做对了。
var contentHeight = ["Height1", "Height2", "Height3", "Height4", "Height5", "Height6"];
var contentSelection = 0;
var Height1 = 200;
var Height2 = 400;
var Height3 = 600;
var Height4 = 800;
var Height5 = 1000;
var Height6 = 1200;
function someFunction(){
$('#content').animate({'height' : contentHeight[contentSelection] +'px'},500);
}
谢谢,
var Height1 = 200;
var Height2 = 400;
var Height3 = 600;
var Height4 = 800;
var Height5 = 1000;
var Height6 = 1200;
var contentHeight = [Height1, Height2, Height3, Height4, Height5, Height6];
var contentSelection = 0;
function someFunction(){
$('#content').animate({'height' : contentHeight[contentSelection] +'px'},500);
}
或者只是这样:
contentHeight = [];
for (var i = 200; i <= 1200; i+=200) {
contentHeight.push(i);
}
var contentSelection = 0;
function someFunction(){
$('#content').animate({'height' : contentHeight[contentSelection] +'px'},500);
}
尽管这可行,但您(在我看来)应该针对这种情况使用不同的方法。使用索引访问值并不能为您提供有关所获得值的任何线索,因此您可能需要根据需要更改代码(和数组结构)。
似乎缺少数组 contentHeight 来保存您的值。也许这只是假设。我添加它是为了完成,但它应该可以工作。
我个人也会考虑字典类型的对象。
var Height1 = 200;
var Height2 = 400;
var Height3 = 600;
var Height4 = 800;
var Height5 = 1000;
var Height6 = 1200;
var contentHeightArray1 = [200,400, 600, 800, 1000, 1200];
var contentHeightArray2 = [Height1,Height2, Height3, Height4, Height5, Height6 ];
var contentHeightDictionary = {
'Height1':200,
'Height2':400,
'Height3':600,
'Height4':800,
'Height5':1000,
'Height6':1200
};
function someFunction(in_params){
$('#content').animate({'height' :in_params.contentList[in_params.heightIndex] +'px'},500);
}
var contentSelection = 0;
someFunction({contentList:contentHeightArray1, heightIndex:contentSelection});
contentSelection = 3;
someFunction({contentList:contentHeightArray2, heightIndex:contentSelection});
contentSelection = 'Height5';
someFunction({contentList:contentHeightDictionary, heightIndex:contentSelection});
someFunction({contentList:contentHeightDictionary, heightIndex:'Height6'});
那么,我有一个问题。该函数应该获取#content 数组第一个变量值的高度。 我尝试了很多不同的方法,但我做对了。
var contentHeight = ["Height1", "Height2", "Height3", "Height4", "Height5", "Height6"];
var contentSelection = 0;
var Height1 = 200;
var Height2 = 400;
var Height3 = 600;
var Height4 = 800;
var Height5 = 1000;
var Height6 = 1200;
function someFunction(){
$('#content').animate({'height' : contentHeight[contentSelection] +'px'},500);
}
谢谢,
var Height1 = 200;
var Height2 = 400;
var Height3 = 600;
var Height4 = 800;
var Height5 = 1000;
var Height6 = 1200;
var contentHeight = [Height1, Height2, Height3, Height4, Height5, Height6];
var contentSelection = 0;
function someFunction(){
$('#content').animate({'height' : contentHeight[contentSelection] +'px'},500);
}
或者只是这样:
contentHeight = [];
for (var i = 200; i <= 1200; i+=200) {
contentHeight.push(i);
}
var contentSelection = 0;
function someFunction(){
$('#content').animate({'height' : contentHeight[contentSelection] +'px'},500);
}
尽管这可行,但您(在我看来)应该针对这种情况使用不同的方法。使用索引访问值并不能为您提供有关所获得值的任何线索,因此您可能需要根据需要更改代码(和数组结构)。
似乎缺少数组 contentHeight 来保存您的值。也许这只是假设。我添加它是为了完成,但它应该可以工作。
我个人也会考虑字典类型的对象。
var Height1 = 200;
var Height2 = 400;
var Height3 = 600;
var Height4 = 800;
var Height5 = 1000;
var Height6 = 1200;
var contentHeightArray1 = [200,400, 600, 800, 1000, 1200];
var contentHeightArray2 = [Height1,Height2, Height3, Height4, Height5, Height6 ];
var contentHeightDictionary = {
'Height1':200,
'Height2':400,
'Height3':600,
'Height4':800,
'Height5':1000,
'Height6':1200
};
function someFunction(in_params){
$('#content').animate({'height' :in_params.contentList[in_params.heightIndex] +'px'},500);
}
var contentSelection = 0;
someFunction({contentList:contentHeightArray1, heightIndex:contentSelection});
contentSelection = 3;
someFunction({contentList:contentHeightArray2, heightIndex:contentSelection});
contentSelection = 'Height5';
someFunction({contentList:contentHeightDictionary, heightIndex:contentSelection});
someFunction({contentList:contentHeightDictionary, heightIndex:'Height6'});