jquery - 传递 jQuery.show() 和 jQuery.hide() 作为要应用于元素的参数

标签 jquery show-hide function-parameter

正如标题所示,我想将 show() 和 hide() jQuery 函数作为另一个函数中的参数传递。

function applyActionOnClass(attr, action) {
    $('#salesonhold_table tr').each(function () {
    if ($(this).attr('status') == attr)
        $(this).action;
    });
}

这是我调用我的applyActionOnClass的时候功能:

$(document).on('change', '.checkboxes', function () {
    var attr = $(this).val();
    if ($(this).is(':checked'))
        applyActionOnClass(attr, show());
    else
        applyActionOnClass(attr, hide());
});

为了稍微解释一下这样做的目的,我有一个表及其每个 <tr>有一个status使它们彼此不同的属性。我在表格顶部有两个复选框,每个可能的复选框 status值,我想隐藏/显示相应的 <tr>当复选框被触发时。

触发器和东西工作正常,但是当涉及到$(this).action;时它说hide is not defined 。知道我在那里做错了什么吗?

非常感谢!

最佳答案

另一个解决方案是像这样改变你的功能

function applyActionOnClass(attr, action) {
    $('#salesonhold_table tr').each(function () {
    if ($(this).attr('status') == attr)
        $(this)[action]();
    });
}

注意$(this)[action]();你可以在这里看到它的工作http://jsfiddle.net/t6rEE/作为概念证明(我尝试了 hideshow)

关于jquery - 传递 jQuery.show() 和 jQuery.hide() 作为要应用于元素的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598587/

相关文章:

html - 使用 before & content Vs 动态切换内容外观。 div 和显示 - 仅 CSS

javascript - 使用键的值作为 Javascript 函数参数中键值对的键

javascript - 如何将 ajax $.get 值传递给 jQuery 中的另一个 $.get 函数?

javascript - 使用 jQuery,您如何只查找可见元素并单独保留隐藏元素?

javascript - 如何查找用户在 beforeunload 警报后是否选择了 “Stay” 或 “Leave” 按钮?

JavaScript:如何显示所有 div 直到选中一个

javascript - 在 JavaScript 中 : Is a function parameter variable (at the moment of function declaration) equivalent to declaring a variable?

c - 为什么函数的 VLA 数组参数使用星号 "[*]"而不是整数?

jquery - 将 Angular 2 组件附加到特定 DOM

javascript - setTimeout - 计数器以指数方式增加速度