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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM