目前我有一个文件,我命名为JQuery.ext.js,其中包括我的所有页面。在这个文件中我有许多功能是不喜欢的东西下面,包含jQuery ext函数的最佳做法是什么?
(function($) {
/**
* Checks to see if a container is empty. Returns true if it is empty
* @return bool
*/
$.fn.isEmptyContainer = function() {
//If there are no children inside the element then it is empty
return ($(this).children().length <= 0) ? $(this) : false;
};
/**
* Strip html tags from elements
* @return jQuery
*/
$.fn.stripHTML = function() {
var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
//Loop through all elements that were passed in
this.each(function() {
$(this).html($(this).html().replace(regexp, ""));
});
return $(this);
};
/**
* This function will check the length of a textarea and not allow the user to go beyond this length.
* You should use the onkeypress event to trigger this function
*
* @param event event This value should always be event when passing it into this function
* @param maxlength int The maximum amount of character that the user is allowed to type in a textarea
*/
$.fn.enforceMaxLength = function(event, maxlength) {
//Only allow the client code to use the onkeypress event
if(event.type == 'keypress') {
//If the client code does not pass in a maxlength then set a default value of 255
var charMax = (!maxlength || typeof(maxlength) != "number") ? 255 : maxlength;
//If the user is using the backspace character then allow it always
if(event.which == 8) { return true; }
//Else enforce the length
else { return ($(this).val().length <= charMax); }
}
//Will only get here if the event type is not keypress
return false;
};
})(jQuery);
是否有另一种方式做到这一点?或者这是大多数人这样做的方式?
感谢您的帮助,新都
EDITED
添加不同的插件的代码并删除了验证插件
感谢您的帮助favio。实际上,这里不仅仅是验证功能。这些恰好是该文件中的前几个。你说我实际上在这里使用插件,是的,我已经将它们设置为插件格式,但是你认为我应该以不同的方式做到这一点吗?我将在这里添加一些其他函数,并删除验证函数以向您展示我需要组织的一些其他函数。 – Metropolis 2010-05-17 12:55:48
没问题。 :)我已经更新了我的答案,希望有帮助! – Favio 2010-05-17 15:05:25
太棒了!非常感谢Favio的帮助。我相信你能帮助我理解,如果我至少在某种程度上是正确的。我会试着更多地思考这个问题,并找到一种在逻辑庄园中区分这些问题的方法。 – Metropolis 2010-05-17 16:09:11