Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery传递要在参数中执行的函数的名称_Javascript_Jquery_Function_Variables - Fatal编程技术网

Javascript 使用jQuery传递要在参数中执行的函数的名称

Javascript 使用jQuery传递要在参数中执行的函数的名称,javascript,jquery,function,variables,Javascript,Jquery,Function,Variables,我有一个在HTML中单击选项卡时调用的函数。我想为该选项卡的onClick()操作中的每个选项卡传递一个唯一的函数名,并且特定函数填充选项卡空间的HTML内容。到目前为止,我的代码是: 当我尝试执行它时,我得到: selectedFunction不是一个函数 没错selectedFunction不是函数,而是包含函数名称的变量。如何调用该变量中包含的函数名?我正在测试仓库的可用性 请看这里: <html> <head> <script> function fo

我有一个在HTML中单击选项卡时调用的函数。我想为该选项卡的
onClick()
操作中的每个选项卡传递一个唯一的函数名,并且特定函数填充选项卡空间的HTML内容。到目前为止,我的代码是:

当我尝试执行它时,我得到:

selectedFunction不是一个函数

没错
selectedFunction
不是函数,而是包含函数名称的变量。如何调用该变量中包含的函数名?我正在测试仓库的可用性

请看这里:

<html>
<head>
<script>
function form_selectTab(selectedTab, selectedFunction, item_id){
    $('.form_tab').css('background-color', 'rgb(222,222,222)');
    $(selectedTab).css('background-color', 'white');
    $('#form_tabSpace').html(selectedFunction(item_id));
}
function warehouse_availability(item_id){
    return('This is warehouse availability for ' + item_id);
}
</script>
</head>
<body>
<div>
    <table style="width: 100%; border-collapse: collapse;" cellspacing="0" cellpadding="0">
        <tr>
            <td class=form_tab onclick="form_selectTab(this, 'warehouse_availability', 'Items Name');">
                Warehouse availability
            </td>
            <td class=form_tab onclick="form_selectTab(this, 'manufacturer_data', 'Items Name');">
                Manufacturer data
            </td>
            <td class=form_tab onclick="form_selectTab(this, 'replacement_skus', 'Items Name');">
                Replacement SKUs
            </td>
            <td class=form_tab onclick="form_selectTab(this, 'similar_items_in_stock', 'Items Name');">
                Similar items in stock
            </td>
            <td class=form_tab onclick="form_selectTab(this, 'pricing', 'Items Name');">
                Pricing
            </td>
            <td class=form_tab onclick="form_selectTab(this, 'special_quotes', 'Items Name');">
                Special Quotes
            </td>
        </tr>
        <tr>
            <td colspan=6 id="form_tabSpace">
            </td>
    </table>
</div>
</body>
</html>

功能窗体\u选择选项卡(选择选项卡、选择功能、项目\u id){
$('form_tab').css('background-color','rgb(22222'));
$(selectedTab).css('background-color','white');
$('#form_tabSpace').html(selectedFunction(item_id));
}
功能仓库可用性(物料id){
退货('这是'+物料id'的仓库可用性);
}
仓库可用性
制造商数据
更换SKU
库存中的类似物品
定价
特别报价

变量
selectedFunction
是一个字符串,因此应将其称为:

window["function_name"](arguments);
因此,在您的情况下,它将是:

window[selectedFunction](item_id);
请查看


希望这有帮助。

函数名是变量,而不是字符串,它们不应该放在引号中

        <td class=form_tab onclick="form_selectTab(this, warehouse_availability, 'Items Name');">


如果它是一个全局函数,则它是
窗口
对象的一个属性,因此您可以执行
窗口[selectedFunction](项目id)。为什么不直接传递函数而不是字符串呢?我了解到这基本上是正确的方法。见巴尔马的回答。我还更新了函数中相应对象的内容,而不是将函数结果返回到对象的.html属性中。我刚刚意识到我做得完全错误,你是对的!函数应该设置TD的内容,而不是相反。谢谢