jQuery 수업

jQuery-13(set적용-attr())

줄라이퍼스트 2020. 2. 19. 15:33

※ attr() 사용

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                
                // 1. 속성값 1개 수정방법
                /* $("#naver").attr("href", "https://www.naver.com");*/

                // 2. 속성값을 여러개 수정방법(object 사용)
                /* 
                    $("#naver").attr({
                    "href" : "https://www.naver.com",
                    "title" : "Welcom naver"
                    }) 

                 */

                // 3. callback함수 이용하여 속성값 수정
                $("#naver").attr("href", function(i,origValue){
                    return origValue + "/jquery/";
                });
            });
        });
    </script>

</head>
<body>
    
    <p><a href="https://www.naver.com" id="naver">naver.com</a></p>
    <button>Change href Value</button>
    <p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p>

</body>
</html>

 

※ 실행화면

실행화면(속성값 변경) - 클릭하시면 자세히 볼 수 있습니다.

 

'jQuery 수업' 카테고리의 다른 글

jQuery-16(remove()사용)  (0) 2020.02.19
jQuery-14(add적용 - append())  (0) 2020.02.19
jQuery-12(set적용- text(), html())  (0) 2020.02.19
jQuery-11(get적용- text(), html())  (0) 2020.02.19
jQuery-10(css- color 적용)  (0) 2020.02.19