투케이2K

65. (jquery/제이쿼리) event preventDefault 사용해 a 태그 링크 이동 기본 이벤트 막기 실시 본문

Jquery

65. (jquery/제이쿼리) event preventDefault 사용해 a 태그 링크 이동 기본 이벤트 막기 실시

투케이2K 2022. 12. 2. 12:38
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : jquery

 

[소스 코드]

    <!-- [CDN 라이브러리 설치] -->
    <script src="https://code.jquery.com/jquery-latest.min.js"></script>





    <!-- [내부 자바스크립트 J쿼리 이벤트 지정] -->
    <script>


        /**
         * --------------------------------
         * [요약 설명]
         * --------------------------------
         * 1. load : html 최초 로드 수행 시 호출 되는 함수입니다
         * --------------------------------
         * 2. on : jquery 에서 이벤트를 적용할 때 사용합니다
         * --------------------------------
         * 3. preventDefault : 기본 태그 속성 이벤트 동작을 막습니다
         * --------------------------------
        */



        // [html 최초 로드 및 이벤트 상시 대기 실시]
        $(window).load(function(){
            console.log("");
            console.log("=========================================");
            console.log("[window load] : [html 로드 수행 실시]");
            console.log("=========================================");
            console.log("");

            // [테스트 함수 호출]
            testMain();
        });



        // [테스트 자바스크립트 함수]
        function testMain(){
            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [start]");
            console.log("=========================================");
            console.log(""); 


            // [on 사용해 이벤트 적용 실시]
            $("#a_Tag").on("click", function(event) { 
                console.log("");
                console.log("=========================================");
                console.log("[testMain] : [click] : a 태그 클릭 발생");
                console.log("=========================================");
                console.log(""); 

                // [a 태그 기본 동작 링크 이동 막음]
                event.preventDefault();  

            });
        };


    </script>
​

<body>


    <!-- [a 태그 선언 실시] -->
    <a id="a_Tag" href="https://www.naver.com">네이버 이동</a>


</body>
 

[결과 출력]

 

 

반응형
Comments