2

一緒に元の変数の名前を作る 2 つの変数を組み合わせることによって、既に宣言されている変数と同じ値に新しい変数を設定しようとしています...これは紛らわしいかもしれませんので、ここに例を示します:

// 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 を書き込みたいと思います...これを行う方法はありますか? これはおそらく、これを行うことについて考え始める正しい方法ではないことを知っています.

ご協力いただきありがとうございます。

4

4 に答える 4

5

これらがグローバル変数の場合、次のようにできます。

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

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

これが機能するのは、すべてのグローバル変数が実際にはwindowオブジェクトのプロパティであり、window オブジェクトのプロパティをwindow.positionまたはとして参照できるからですwindow["position"]"position"後者が機能するため、上記の例のように文字列操作を使用して文字列を構築することもできます。

なぜこれをしているのかと尋ねますか?人々がこれを行うように求める一般的な理由の 1 つは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]);
}

解決しようとしている実際の問題を説明していただければ、それを解決する最善の方法をお勧めします。あなたが現在尋ねていることは、ほとんどすべての問題に対する間違ったアプローチのように聞こえます。

于 2012-01-19T17:23:44.807 に答える
4

なぜこれを行っているのかわかりませんが、evalメソッドを使用して可能です。

これを試して

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

document.write (eval(finalVar));

デモ

于 2012-01-19T17:25:10.047 に答える
1
var position = 'top';
var pos = "posi";
var tion = "tion";
var finalVar = pos+tion;

alert(window[finalVar]); // better that `eval()`
document.write(window[finalVar]);
于 2012-01-19T17:32:30.697 に答える
0

変数 parseInt(pos)+parseInt(ition) を組み合わせるときにこれを使用します 文字列変数 pos と ition を宣言することを忘れないでください... :)

于 2012-01-19T17:27:32.970 に答える