简体   繁体   中英

how to call function using var name?

I have a div that needs to move to a specific position depending on which dropdown div is clicked. I made a function for each one. the functions names corresponds with the dropdown divs id + "Arrow". I then made a var that got the id name of the clicked dropdown div and added the word arrow, this way it would match the function name. when I tried to use the var to call the function it does not work.

 var arrowName = $(this).attr('id') + "Arrow()";

    function btn1s1Arrow() {$( "#greenArrow" ).animate({top: "203px", left: "291px"}, "normal" );}
    function btn1s2Arrow() {$( "#greenArrow" ).animate({top: "345px", left: "381px"}, "normal" );}
    function btn1s3Arrow() {$( "#greenArrow" ).animate({top: "444px", left: "391px"}, "normal" );}

   //////dont mind this $("#qwrapper .accordionContent2").slideUp('normal'); 
   ////////dont mind this $(this).next().slideDown('normal');

    arrowName();

help would be much appreciated. thanks in advanced... i am so stuck

here is a portion of the code, so what i said actually makes sense http://jsfiddle.net/vrs9j/1/

Well there is no need for that, you could do this:

var arrowName = $(this).attr('id');

var animProps = {
    btn1s1: [203, 291],
    btn1s2: [345, 381],
    btn1s3: [444, 391]
};

var animProp = animProps[arrowName];
$("#greenArrow").animate({top: animProp[0], left: animProp[1]}, "normal");

您可以执行以下操作:

window[arrowName]();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM