本文介绍了重新定义一个jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要重新定义jQuery val()函数,但我不想混淆源代码。具体来说,我需要这样做: $(div.smth) .val = function(){return this.innerText;};
。但是,这个代码没有修改val函数。我做错了什么?解决方案
你应该改变原型的功能(jQuery调用这个 FN
)。这是所有功能像 $(...),某些
继承自。
$。fn.val = function(){...};
如果要保存原始功能:
var old = $ .fn.val; //`old`不会被覆盖
$ .fn.val = function(){...};
I need to redefine the jQuery val() function, but I don't want to mess with the source code. Is it possible to modify it on just one element?
Specifically, I need to do something like this:$("div.smth").val = function() {return this.innerText;};
. However, the val function is not modified by this code. What am I doing wrong?
解决方案
You should instead modify the function of the prototype (jQuery calls this fn
). This is where all functions like $(...).something
inherit from.
$.fn.val = function() { ... };
If you want to save the original function:
var old = $.fn.val; // `old` won't be overwritten
$.fn.val = function() { ... };
这篇关于重新定义一个jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!