使用 javascript 组合两个变量以将新变量设置为已声明的变量
我试图通过组合两个变量来将新变量设置为与已声明的变量相同的值,这两个变量一起构成原始变量的名称...这可能听起来令人困惑,所以这里有一个示例:
// 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;
});
}
});
});
然后我希望 5 成为写入文档...有什么办法可以做到这一点吗?我知道这可能不是开始考虑这样做的正确方法,我只是将它用作我想要做的事情的同义词。
感谢您的帮助。
I am trying to set a new variable to the same value as an already declared variable by combining two variables that together make the name of the original variable... This may sound confusing, so here's an example:
// 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;
});
}
});
});
I would then want 5 to be written to the document... is there any way of doing this? I know this is probably not the correct way to even begin thinking about doing this, i was just using it as a synonym for what i was trying to do.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这些是全局变量,您可以这样做:
这之所以有效,是因为所有全局变量实际上都是
window
对象的属性,并且您可以将 window 对象的属性作为window.位置
或窗口[“位置”]
。由于后者有效,您还可以使用字符串操作构造字符串"position"
,如上例所示。我想问你为什么要这样做?人们要求这样做的一个常见原因是他们可以访问诸如
position1
、position2
等变量......如果是这种情况,那么更好的答案是使用可以通过索引访问的数组:您也可以通过变量访问数组值,如下所示:
或者,迭代整个数组:
如果您描述了您要解决的实际问题,我们可以推荐最好的方法解决它。您目前所问的问题听起来几乎是解决任何问题的错误方法。
If these are global variables, you can do it like this:
This works only because all global variables are actually properties of the
window
object and you can reference properties of the window object either aswindow.position
orwindow["position"]
. Since the latter works, you can also construct the string"position"
using string operations as in the example above.I would ask why you're doing this? One common reason people ask to do this is so they can access variables like
position1
,position2
, etc.... If that's the case, then the better answer is to use an array which can be accessed by index:You can access array values via a variable too like this:
Or, to iterate over the entire array:
If you describe the real problem you're trying to solve, we can recommend the best way to solve it. What you're currently asking about sounds like the wrong approach to pretty much any problem.
我不知道你为什么这样做,但是可以使用
eval
方法。试试这个
演示
I don't know why you are doing this but yes it is possible using
eval
method.Try this
DEMO
在组合变量 parseInt(pos)+parseInt(ition) 时使用它
不要忘记声明字符串变量 pos 和 ition...:)
use this while combining your variables parseInt(pos)+parseInt(ition)
dont forget to declare string variables pos and ition... :)