zoukankan      html  css  js  c++  java
  • js中将字符串作为函数名来调用的方法

    方法:eval()

    eg:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>将字符串作为函数名调用函数</title>
    </head>
    <body>
    <span>指标:</span>
    <select id="zhibiao">
              <option selected="" value="总体">总体</option>
              <option value="queryManagementVolume">办理量</option>
              <option value="queryTimeConsuming">平均耗时</option>
              <option value="queryOvertimePercent">超时占比</option>
    </select>
    </body>
    <script src="../js/jquery.js"></script>
    <script>
    $(document).on("change",'select#zhibiao',function(){
            var fn=$(this).val();
            /* fn(); */
            eval(fn+"()");
    });
    function queryManagementVolume(){
        console.log("查询办理量");
    }
    function queryTimeConsuming(){
        console.log("查询平均耗时");
    }
    function queryOvertimePercent(){
        console.log("查询超时占比");
    }
    
    </script>
    </html>
    var slideArr=new Array(
        'slideLeft',
        'slideTop',
        'slideRight',
        'slideBottom',
        'slideLeftTop',
        'slideTopRight',
        'slideRightBottom',
        'slideBottomLeft'
    );
    
    function slideRandom(arr=slideArr,width,height,time) {
        var length=slideArr.length;
        var index=Math.floor(Math.random()*length);
        var functionName=slideArr[index];
        //alert(functionName);
        eval(functionName+'(width,height,time)');
    }
  • 相关阅读:
    HTML5 Input 类型
    Html5 web 储存
    解决json日期格式问题的3种方法(转载)
    Json格式串处理
    全局图片防盗链处理
    我的博客开张了
    iPhone手机屏幕分辨率
    通过CSS3伪类,美化Radio按钮样式
    测试用例 相关
    MongoDB基本命令
  • 原文地址:https://www.cnblogs.com/juicy-initial/p/9961237.html
Copyright © 2011-2022 走看看