正如标题所示,我想将 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/作为概念证明(我尝试了 hide
和 show
)
关于jquery - 传递 jQuery.show() 和 jQuery.hide() 作为要应用于元素的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598587/