λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
JAVASCRIPT/jQuery

jQuery | 속성 λ©”μ„œλ“œ

by μ½”λ”©ν•˜μž9 2022. 9. 4.
728x90

πŸ“ jQuery 속성 λ©”μ„œλ“œμ— λŒ€ν•΄ μ•Œμ•„λ΄…μ‹œλ‹€ !

πŸ“‚ attr() λ©”μ„œλ“œ

μ„ νƒν•œ μš”μ†Œμ˜ attribute(속성)λ₯Ό 선택, 생성, λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ‹€ν–‰ λΆ„λ₯˜ ν˜•μ‹
취득 $("a").attr("href");
생성, λ³€κ²½ $("a").attr("href", "http://www.google.com/").attr("target", "_blank");
$("a").attr({href: "http://www.google.com/", target: "_blank"});
콜백 ν•¨μˆ˜ $("a").attr("href", function(index, h) {
    // indexλŠ” 각 a μš”μ†Œμ˜ index 0,1,2
    // hλŠ” 각 a μš”μ†Œ href 속성
    return attribute (속성) // 각 a μš”μ†Œμ˜ 속성을 생성 및 변경함.
});
...
<a href="http://www.daum.net" target="_blank" title="μƒˆμ°½">λ‹€μŒ</a>
<a href="http://www.naver.com" target="_blank" title="μƒˆμ°½">넀이버</a>
<a href="http://www.nate.com" target="_blank" title="μƒˆμ°½">λ„€μ΄νŠΈ</a>

πŸ“’ 직접 μ μš©ν•˜λ©΄μ„œ μ΄ν•΄ν•˜μž !

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>attr() λ©”μ„œλ“œ<title>
    <script src="jquery-3.3.1.min.js"></script>
    <script>
        $(document).ready(function() {
            console.log($("#site > a:eq(0)").attr("href"));
            $("#site > a:eq(1)").attr("href", "http://m.naver.com").text("넀이버 λͺ¨λ°”일");
            $("#site a").attr("title", function() {
                return "μƒˆμ°½";
            });
        });
    </script>
</head>
<body>
    <div id="site>
        <a href="http://www.daum.net" target="_blank">λ‹€μŒ</a>
<a href="http://www.naver.com" target="_blank">넀이버</a>
<a href="http://www.nate.com" target="_blank">λ„€μ΄νŠΈ</> </di> </body> </html>

πŸ“‚ prop() λ©”μ„œλ“œ

prop() λ©”μ„œλ“œλŠ” μš”μ†Œμ˜ 속성을 true/false둜 μ œμ–΄ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

attr(); html attribute(속성) κ΄€λ ¨ λ©”μ„œλ“œ

prop(); μžλ°”μŠ€ν¬λ¦½νŠΈ property(ν”„λ‘œνΌν‹°) κ΄€λ ¨ λ©”μ„œλ“œ

πŸ“’ 직접 μ μš©ν•˜λ©΄μ„œ μ΄ν•΄ν•˜μž !

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>prop() λ©”μ„œλ“œ<title>
    <script src="jquery-3.3.1.min.js"></script>
    <script>
        $(document).ready(function() {
            console.log($("input:checkbox").eq(0).attr("checked"));
            console.log($("input:checkbox").eq(1).prop("checked"));
            $("input:checkbox").eq(0).attr("checked", "checked");
            $("input:checkbox").eq(1).prop("checked", true);
            console.log($("input:checkbox").eq(0).attr("checked"));
            console.log($("input:checkbox").eq(1).prop("checked"));
        });
    </script>
</head>
<body>
    <input type="checkbox" id="html"><label for="html">html</label>
    <input type="checkbox" id="css"><label for="css">css</label>
</body>
</html>

'JAVASCRIPT > jQuery' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

jQuery | μŠ€νƒ€μΌ λ©”μ„œλ“œ  (2) 2022.09.04
jQuery | 클래슀 λ©”μ„œλ“œ  (2) 2022.09.04
jQuery | 탐색 μ„ νƒμž  (8) 2022.08.31
jQuery | ν•„ν„° μ„ νƒμž  (7) 2022.08.31
jQuery | 속성 μ„ νƒμž  (9) 2022.08.30

λŒ“κΈ€


It's cording time

μ½”λ”© μ—¬κΈ°μ„œ μ •λ¦¬ν•˜κ³  λ°°μ›Œλ³΄μžκ΅¬ :9

κ΄‘κ³  μ€€λΉ„μ€‘μž…λ‹ˆλ‹€.