我已经意识到有关于此的类似问题,但似乎无法使其发挥作用。
请告诉我需要做什么。
示例:(其中字符串“test”是函数名称)
<script>
function test(){
alert("Hello World");
}
//is this the right way to call it?
window["test"]();
</script>
//没有评估
答案 0 :(得分:1)
请查看以下
var fn = window["test"];
if(typeof fn == 'function') {
fn();
}
答案 1 :(得分:0)
Eval就是您所需要的。例如:
<script>
eval("alert('hello')");
</script>
答案 2 :(得分:0)
试试这个
<script>
function test(){
alert("Hello World");
}
var func = 'test'
this[func]();
</script>
答案 3 :(得分:0)
你的例子应该有用。这是对类似问题的回答 How to execute a JavaScript function when I have its name as a string
答案 4 :(得分:0)
Pawan的答案是对的,因为小提琴将代码包装到$(window).on(“load”)事件中,所以它在小提琴中不起作用。在普通页面中尝试一下。对不起,不能骂,声誉低