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> |
attr() λ©μλ : μμ μ½λ
<!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">λ€μ΄νΈ</a>
</div>
</body>
</html>
결과 보기
<div id="site">
<a href="http://www.daum.net" target="_blank" title="μμ°½">λ€μ</a>
<a href="http://m.naver.com" target="_blank" title="μμ°½">λ€μ΄λ² λͺ¨λ°μΌ</a>
<a href="http://www.nate.com" target="_blank" title="μμ°½">λ€μ΄νΈ</a>
</div>
μ½λν κ²°κ³Ό
prop() λ©μλ
attr()κ° html attribute(μμ±)μ κ΄λ ¨λ λ©μλλΌλ©΄ prop()μ μλ°μ€ν¬λ¦½νΈ νλ‘νΌν°(property)μ κ΄λ ¨λ λ©μλμ΄λ€. prop() λ©μλλ μμμ μμ±μ true/falseλ‘ μ μ΄ κ°λ₯νλ€.
prop() λ©μλ : μμ μ½λ
<!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>
결과 보기
<input type="checkbox" id="html" checked="checked">
<label for="html">html</label>
<input type="checkbox" id="css">
<label for="css">css</label>
// undefined
// false
// checked
// true
μ½λν κ²°κ³Ό
attr() λ©μλλ checked μμ±μ΄ μμμ μΆκ°λλ λ°λ©΄, prop() λ©μλμ trueλ μλ°μ€ν¬λ¦½νΈμ νλ‘νΌν°λ‘ μ²λ¦¬λ¨μ μ μ μλ€. Google Chromeμμ κ²μ¬(F12) > Console μ°½μμ νμΈ κ°λ₯.
'jQuery' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[jQuery] μ€νμΌ κ΄λ ¨ λ©μλ μμ보기 (5) | 2022.09.03 |
---|---|
[jQuery] ν΄λμ€ κ΄λ ¨ λ©μλ μμ보기 (6) | 2022.09.03 |
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) - νμ μ νμ (3) | 2022.08.30 |
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) - νν° μ νμ (3) | 2022.08.30 |
[JavaScript] jQuery(μ μ΄μΏΌλ¦¬) - μμ± μ νμ (4) | 2022.08.30 |
λκΈ