현재 모든 페이지에 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);
다른 방법이 있습니까? 또는 이것이 대부분의 사람들이하는 방식입니까? 어떤 도움
감사합니다, 메트로 폴리스
유효성 검사 플러그인
도움 주셔서 감사합니다. 사실 여기에는 단순히 검증 기능 이상이 있습니다. 이것들은 그 파일의 처음 몇 가지 일뿐입니다. 당신은 내가 실제로 플러그인을 사용하고 있다고 말했고, 그렇습니다. 플러그인 형식으로 설정했는데, 다른 방식으로해야한다고 생각합니까? 여기에 다른 함수를 넣고 유효성 검사를 제거하여 정리해야하는 다른 기능을 보여 드리겠습니다. – Metropolis
문제 없습니다. :) 나는 내 대답을 업데이 트했습니다, 도움이 희망! – Favio
좋습니다! Favio의 도움에 많은 시간을드립니다. 나는 적어도 내가 올바른 방향으로 가고 있는지 이해할 수 있도록 도왔습니다.나는 이것에 대해 좀 더 생각해보고 논리 매너에서 이들을 분리하는 방법을 찾으려고 노력할 것이다. – Metropolis