使用 javascript 组合两个变量以将新变量设置为已声明的变量

发布于 2024-12-27 21:47:21 字数 4944 浏览 3 评论 0原文

我试图通过组合两个变量来将新变量设置为与已声明的变量相同的值,这两个变量一起构成原始变量的名称...这可能听起来令人困惑,所以这里有一个示例:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝不服输 2025-01-03 21:47:21

如果这些是全局变量,您可以这样做:

var position = 4;
var a = "posi";
var b = "tion";

document.write(window[a+b]);

这之所以有效,是因为所有全局变量实际上都是 window 对象的属性,并且您可以将 window 对象的属性作为 window.位置窗口[“位置”]。由于后者有效,您还可以使用字符串操作构造字符串 "position",如上例所示。

我想问你为什么要这样做?人们要求这样做的一个常见原因是他们可以访问诸如 position1position2 等变量......如果是这种情况,那么更好的答案是使用可以通过索引访问的数组:

var positions = [1,4,67,99];

document.write(positions[3]);

您也可以通过变量访问数组值,如下所示:

var positions = [1,4,67,99];
var pos = 3;

document.write(positions[pos]);

或者,迭代整个数组:

var positions = [1,4,67,99];

for (var i = 0; i < positions.length; i++) {
    document.write(positions[i]);
}

如果您描述了您要解决的实际问题,我们可以推荐最好的方法解决它。您目前所问的问题听起来几乎是解决任何问题的错误方法。

If these are global variables, you can do it like this:

var position = 4;
var a = "posi";
var b = "tion";

document.write(window[a+b]);

This works only because all global variables are actually properties of the window object and you can reference properties of the window object either as window.position or window["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:

var positions = [1,4,67,99];

document.write(positions[3]);

You can access array values via a variable too like this:

var positions = [1,4,67,99];
var pos = 3;

document.write(positions[pos]);

Or, to iterate over the entire array:

var positions = [1,4,67,99];

for (var i = 0; i < positions.length; i++) {
    document.write(positions[i]);
}

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.

尤怨 2025-01-03 21:47:21

我不知道你为什么这样做,但是可以使用 eval 方法。

试试这个

var position = 5;
var pos = "posi";
var tion = "tion";
var finalVar = pos+tion;

document.write (eval(finalVar));

演示

I don't know why you are doing this but yes it is possible using eval method.

Try this

var position = 5;
var pos = "posi";
var tion = "tion";
var finalVar = pos+tion;

document.write (eval(finalVar));

DEMO

人心善变 2025-01-03 21:47:21
var position = 'top';
var pos = "posi";
var tion = "tion";
var finalVar = pos+tion;

alert(window[finalVar]); // better that `eval()`
document.write(window[finalVar]);
var position = 'top';
var pos = "posi";
var tion = "tion";
var finalVar = pos+tion;

alert(window[finalVar]); // better that `eval()`
document.write(window[finalVar]);
海螺姑娘 2025-01-03 21:47:21

在组合变量 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... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文