我可以创建一个属于这种类型函数的变量吗:
jQuery.fn.appendValueToTextArea = function(txt){
return this.each(function(){
this.value += txt;
});
};
?
我尝试在其前面放置一个 var
但出现错误。
最佳答案
如果你想像你在评论中写的那样调用元素上的方法,你肯定必须扩展jQuery.func
。
也许您只想将名称存储在变量中?
var name = 'appendValueToTextArea';
element[name]('text');
唯一的其他方法是存储对该函数的引用并使用 .call()
或.apply()
:
var func = jQuery.fn.appendValueToTextArea = function(txt) {
// ...
};
func.call(element, 'text');
或者(因为我不确定你真正想要什么)你可以先将函数分配给变量,然后将其分配给jQuery.fn.appendValueToTextArea
:
var func = function(txt) {
// ...
};
jQuery.fn.appendValueToTextArea = func;
关于javascript - jQuery 类型函数作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5811135/