combinant deux variables pour définir une nouvelle variable sur une variable déjà déclarée à l'aide de JavaScript

StackOverflow https://stackoverflow.com/questions/8930425

Question

J'essaie de définir une nouvelle variable sur la même valeur qu'une variable déjà déclarée en combinant deux variables qui font ensemble le nom de la variable d'origine ... cela peut sembler déroutant, alors voici un exemple:

// 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;
                        }); 
                }
        });

});

Je voudrais alors que 5 soit écrit dans le document ... y a-t-il un moyen de le faire? Je sais que ce n'est probablement pas la bonne façon de commencer à penser à faire cela, je l'utilisais simplement comme synonyme pour ce que j'essayais de faire.

Merci de votre aide.

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top