본문 바로가기
프론트엔드/javascript

jquery 확장 메서드

by 느바 2024. 7. 6.
반응형

jquery 확장 메서드

 

[셀렉터를 이용한 확장 메소드]
문법 : $.fn.메소드명
사용방법 : $(selector).메소드명()

$.fn.HTMLTagRestore = function(){
            var value = this.val();
            if(this.is("input") || this.is("textarea")){
                this.val(value.replaceAll('a', 'A').replaceAll('b', 'B'));
            }
            return this; // 함수 체인
 };
 $("input").on('input',function(){
 	$(this).HTMLTagRestore();
 });
 $("input").HTMLTagRestore();

 

[실렉터를 이용하지 않는 확장 메소드]
문법 : $.메소드명

사용방법 : $.메소드명()

$.HTMLTagRestore = function(){
            $('input, textarea').each(function(){                
                var value = $(this).val();
                $(this).val(value.replaceAll('a', 'A').replaceAll('b', 'B'));           
            });
};
        
$.HTMLTagRestore();

 

참고

https://annotations.tistory.com/89?category=595292

 

jQuery 사용자 정의 메소드 만들기 ($.fn 확장)

나만의 jQuery 메소드를 만들어 사용해보자! 웹 개발을 하시면서 대부분 유틸리티성 코드들을 한두가지씩 보유하고 계실겁니다. 이번 포스팅에서는 자바스크립트 객체를 만들어서 함수를 추가하

annotations.tistory.com

 

반응형

'프론트엔드 > javascript' 카테고리의 다른 글

PerformanceAPI  (0) 2024.12.31
Querystring  (0) 2024.12.29
한글 입력시 이벤트 중복 호출 해결 방법  (0) 2024.03.09
부동소수점 오류 현상  (0) 2024.03.08
금액 입력 input 구현  (0) 2023.12.29