js或Jquery 根據字串型別的方法名,找到對應的方法並且呼叫
阿新 • • 發佈:2019-02-01
轉自:https://zhidao.baidu.com/question/1733819401878068867.html
可以使用js的eval函式實現,示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<script type= "text/javascript" >
//自定義函式,用於彈出三個引數的值
function alertFunc(str1,str2,str3){
alert(str1);
alert(str2);
alert(str3);
}
//自定義函式:根據傳入的函式名,呼叫函式
function callAlert(functionName){ //根據函式名得到函式型別
var func=eval(functionName);
//建立函式物件,並呼叫
new func(arguments[1],arguments[2],arguments[3]);
}
</script>
<!--編寫按鈕,在點選事件中呼叫函式-->
<button onclick= "callAlert('alertFunc','tom','hello','world')" >測試函式呼叫</button>
|