sim*_*per 2 variables jquery arithmetic-expressions operators
我正在尝试通过将两个变量组合成一个原始变量的名称来将一个新变量设置为与已声明的变量相同的值...这听起来可能令人困惑,所以这里是一个示例:
// JavaScript Document
document.write (finalVar);
$(document).ready(function()
{
var position_1 = $("#box_1").position();
var left_1 = position_1.left;
var top_1 = position_1.top;
var position_2 = $("#box_2").position();
var left_2 = position_2.left;
var top_2 = position_2.top;
var box;
var boxLength;
var boxNumber;
var selected = 0;
$("#box_1").click
(function()
{
if (selected == 1) // if a box is selected run the following
{
box = $(".selected").attr("id");
boxLength = box.length;
boxNumber = box.charAt(boxLength-1); // finds the number of the box
alert(+boxNumber);
if (box == "box_1") // if the selected box is itself then mimimise the box, remove the selected class from it and set selected to zero
{
$("#box_1").animate({height:50,opacity:0.8,left:left_1,top:top_1,borderRadius:4,MozborderRadiu s:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function()
{
$(this).removeClass("selected");
});
selected = 0;
}
else
{
$(".selected").animate({height:50,opacity:0.8,left:left_+boxNumber,top:top_+boxNumber,borderRadius:4,MozborderRadius:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function()
{
$(".selected").removeClass("selected");
$("#box_1").animate({width:900,left:60,top:0,borderRadius:10,MozborderRadius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function()
{
$("#box_1").addClass("selected");
});
}
);} } // end of function for if a box is selected
else // if no box is selected run the following
{
$("#box_1").animate({width:900,left:60,top:0,borderRadius:10,MozborderRadius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function()
{
$("#box_1").addClass("selected");
});
selected = 1;
}
});
$("#box_2").click
(function()
{
if (selected == 1) // if a box is selected run the following
{
box = $(".selected").attr("id");
boxLength = box.length;
boxNumber = box.charAt(boxLength-1); // finds the number of the box
alert(+boxNumber);
if (box == "box_2") // if the selected box is itself then mimimise the box, remove the selected class from it and set selected to zero
{
$("#box_2").animate({height:50,opacity:0.8,left:left_2,top:top_2,borderRadius:4,MozborderRadius:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function()
{
$(this).removeClass("selected");
selected = 0;
});
}
else
{
$(".selected").animate({height:50,opacity:0.8,left:left_+boxNumber,top:top_+boxNumber,borderRadius:4,MozborderRadius:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function()
{
$(".selected").removeClass("selected");
$("#box_2").animate({width:900,left:60,top:0,borderRadius:10,MozborderRadius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function()
{
$("#box_2").addClass("selected");
});
}
);} } // end of function for if a box is selected
else // if no box is selected run the following
{
$("#box_2").animate({width:900,left:60,top:0,borderRadius:10,MozborderRadius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function()
{
$("#box_2").addClass("selected");
selected = 1;
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
然后,我希望将5写入文档中……有什么办法吗?我知道这可能甚至不是开始考虑进行此操作的正确方法,我只是将其用作我尝试执行的操作的同义词。
谢谢你的帮助。
如果这些是全局变量,则可以这样进行:
var position = 4;
var a = "posi";
var b = "tion";
document.write(window[a+b]);
Run Code Online (Sandbox Code Playgroud)
这仅是因为所有的全局变量实际上是属性window对象,你可以参考window对象的属性window.position或window["position"]。由于后者有效,因此您也可以"position"像上面的示例一样使用字符串操作构造字符串。
我会问你为什么要这样做?人们要求这样做的一个常见原因是,他们可以访问诸如position1,position2等等的变量。如果是这种情况,那么更好的答案是使用可以通过索引访问的数组:
var positions = [1,4,67,99];
document.write(positions[3]);
Run Code Online (Sandbox Code Playgroud)
您也可以通过变量访问数组值,如下所示:
var positions = [1,4,67,99];
var pos = 3;
document.write(positions[pos]);
Run Code Online (Sandbox Code Playgroud)
或者,要遍历整个数组:
var positions = [1,4,67,99];
for (var i = 0; i < positions.length; i++) {
document.write(positions[i]);
}
Run Code Online (Sandbox Code Playgroud)
如果您描述了您要解决的实际问题,我们可以推荐解决问题的最佳方法。您当前正在询问的内容听起来几乎是解决任何问题的错误方法。