码迷,mamicode.com
首页 > Web开发 > 详细

js中将字符串作为函数名来调用的方法

时间:2018-11-15 01:41:39      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:length   ott   sel   bottom   over   func   函数名   dex   style   

方法: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)‘);
}

 

js中将字符串作为函数名来调用的方法

标签:length   ott   sel   bottom   over   func   函数名   dex   style   

原文地址:https://www.cnblogs.com/juicy-initial/p/9961237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!