简体   繁体   English

如何调用在另一个函数中作为参数传递的函数

[英]how to call a function which is passed as parameter in another function

how to call a function which is passed as parameter in another function 如何调用在另一个函数中作为参数传递的函数

here the function i am calling onclick i paased the fucntion name as parameter 在这里,我正在调用onclick的函数将函数名称作为参数暂停了

onclick="calldialogbox('thisisfortest')"


this is the function which is calling onclick 这是调用onclick的函数

function calldialogbox(funtion_name){
    var dialog = $('<p>Are you sure?</p>').dialog({
        buttons: {
            "Yes": function(){
                dialog.dialog('close');
                funtion_name(); //here i am calling function
            },
            "Cancel":  function() {
                dialog.dialog('close');
            }
        }
    });
}


this is the function that is i want to call 这是我要调用的功能

function thisisfortest(){
    alert("thisisfortest");
}

Simply pass the function without quotes. 只需传递不带引号的函数即可。 This is called callback. 这称为回调。 In javascript functions are first class objects, so you can pass them around like normal variables. 在javascript函数中,头等对象是一流的,因此您可以像普通变量一样传递它们。

PS. PS。 Try to use addEventListener instead of html onclick attribute. 尝试使用addEventListener而不是html onclick属性。

In case of passing in a function name as parameter - use the following workaround(dealing with 'global' function): 如果将函数名称作为参数传递,请使用以下解决方法(使用'global'函数处理):

...
"Yes": function(){
    dialog.dialog('close');
    window[funtion_name](); //here I'm calling function <----
},
...

Instead of thinking about function_name think about it as simply function intead - you're not passing in the name of the function as a string but an actual reference to the function itself. 与其考虑function_name ,不如简单地认为它是function intead -您不是将函数名称作为字符串传递,而是对函数本身的实际引用。

onclick="calldialogbox(thisisfortest)"

You can do this because functions in JavaScript are "first class objects" and can be assigned to variables, passed in as parameters to other functions (aka callbacks), and returned from functions too. 您可以这样做是因为JavaScript中的函数是“一流的对象”,可以分配给变量,可以作为参数传递给其他函数(也称为回调),也可以从函数中返回。

In terms of JS best practice the best way to approach this is to separate out your inline JS using an event listener. 根据JS最佳实践,解决此问题的最佳方法是使用事件侦听器将内联JS分离出来。 Something like this: 像这样:

HTML 的HTML

<div id="clickable">Click me</div>

JS JS

var div = document.getElementById('clickable');
div.addEventListener('click', calldialogbox(thisisfortest), false);

That way you don't need the onclick event in the HTML. 这样,您就不需要HTML中的onclick事件。

DEMO 演示

(Really, because calldialogbox isn't an async function, there's really no need to pass that callback in at all in this example.) (确实,由于calldialogbox不是异步函数,因此在此示例中,实际上根本不需要传递该回调。)

try this one. 试试这个。
in html 在HTML

<a onclick="calldialogbox(thisisfortest)">Click Me</a>

js js

function calldialogbox(funtion_name){
var dialog = $('<p>Are you sure?</p>').dialog({
    buttons: {
        "Yes": function(){
            dialog.dialog('close');
            funtion_name(); //here i am calling function
        },
        "Cancel":  function() {
            dialog.dialog('close');
        }
    }
});
}

function thisisfortest(){
alert("thisisfortest");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将函数调用为另一个函数作为以字符串形式传递的参数 - How to call a function another function as a parameter passed as a string 我将一个函数(本身具有一个参数)作为参数传递给另一个函数,以及如何获取该参数 - I passed a function(which itself has a parameter) as a parameter into another function and how to get the parameter 在一个函数中传递参数,该参数由另一个函数中的“引用”传递 - Pass a parameter in a function which is passed by “reference” in another function 如何通过作为参数传递的名称调用函数? - How to call a function by its name, passed as parameter? 如何引用传递给函数的另一个参数的“ this”? - How to reference “this” of another parameter passed into function? 如何编写一个JavaScript函数,该函数将调用作为参数传递的另一个函数? - How can I write a JavaScript function that will call another function passed as a parameter? 当在 jquery 中的 function 中将事件作为参数传递时,如何在“keydown”上调用 function - How to call a function on 'keydown' when an event is passed as a parameter in that function in jquery 如何执行将细节作为参数传递给另一个函数的函数 - How to execute a function which details are passed as parm in another function 如何从传递给CasperJS求值的函数中调用另一个函数 - How to call another function from the function that is passed to evaluate in CasperJS 将动态参数添加到带有参数的函数中,该参数作为参数传递给当前函数 - Adding Dynamic Parameter to a function with parameter which is passed as parameter to current function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM