μΉκ³΅λ·°
jQuery μ νμ μ΄ μ§ν© - 곡λΆνλ©΄μ κ³μ μ λ°μ΄νΈ μμ
μ΄λ
Έν€_
2019. 6. 25. 14:28
<script>
$(function(){
//1. $("#IDλͺ
") : html νκ·Έμ IDλ₯Ό μ ννλ€.
var item1 = $("#txt").val();
//2. $(".ν΄λμ€λͺ
") : html νκ·Έμ ν΄λμ€λ₯Ό μ ννλ€.
var item2 = $(".hide").val();
//3. νκ·Έμ μμ±κ°μ μκ³ μΆλ€. (GET)
var attr = $("#txt").attr("type");
//4. νκ·Έμ μμ±κ°μ μ
νκ³ μΆλ€. (SET)
$("#txt").attr("type", "κ°")
//5. νΉμ HTML μμμ HTML κ°μ μκ³ μΆμ λ.
$("#div3").html();
//6. νΉμ HTML μμμ HTML κ°μ μ
νκ³ μΆμ λ.
$("#div3").html("<h1> Hello </h1>");
//7. HTML μ½λ μ체λ₯Ό μκ³ μΆμ λ.
$("#div4").text();
//8. HTML μ½λ μ체λ₯Ό νλ©΄ μΆλ ₯νκ³ μ ν λ.
$("#div4").text("<h1> Hello </h1>");
});
</script>
νΉμ μμΉμ νκ·Έλ₯Ό μ½μ ν λ.
<script>
$(function(){
//νΉμ μμΉμ μ½μ
: before(), after(), perpend(), append()
var elem = '<div><h1> μ½μ
λ λ μ΄μμ </h1></div>';
$("#layer1").after(elem);
$("#layer3").before(elem);
$("#layer2").prepend(elem);
$("#layer2").append(elem);
});
</script>
μ¬λ¬κ°μ§ μ’ λ₯μ Selector(μ νμ)
<script>
$(function(){
/*
class : $(".xxx")
ID : $("#.xxx");
name : $("input[name=xxx]")
class : $("input[class=xxx]") tag[μμ±ν€=μμ±κ°]
$("div:eq(0)") = $("div").eq(0)
*/
var txt1 = $(".text1").val();
var txt2 = $("input[class=text2]").val();
var txt3 = $("input[name=text1]").val();
var txt4 = $("input[name=text2]").val();
var checkedRadio = $("input[name=rdo]:checked").val();
console.log(checkedRadio);
//h2 ν΄λμ€μμ 첫λ²μ§Έ html νκ·Έλ§ λ¦¬ν΄λ¨.
console.log($(".h2class").html());
//eq(n) nλ²μ§Έ html νκ·Έ κ°μ κ°μ Έμ΄.
console.log($(".h2class:eq(1)").html());
});
</script>
CSS μ νμ
<style>
.sample1{
color:yellow;
font-weight:bold;
}
.sample2{
color:orange;
font-weight:bold;
}
</style>
<script>
$(function(){
//css()
/*$("div:eq(0)").css("color", "red");
$("div:eq(0)").css("font-weight", "bold");
$("div:eq(1)").css("color", "blue");
$("div:eq(1)").attr("style", "color:green; font-weight:bold;");*/
//addClass() : ν΄λμ€λͺ
μ μ§μ λ μ
λ ν°μ μΆκ°νλ€.
//removeClass() : ν΄λμ€λͺ
μ μ§μ λ μ
λ ν°μ μ§μ΄λ€.
//hasClass() : ν΄λμ€λͺ
μ΄ μ§μ λ μ
λ ν°λ‘λΆν° μ‘΄μ¬νλμ§ μ¬λΆλ₯Ό νλ¨νλ€.(true/false)
$("#divid1").addClass("sample1");
$("#divid2").addClass("sample2");
$("#divid2").removeClass("sample2");
var isHas = $("#divid2").hasClass("sample2");
console.log(isHas);
if( $("#divid1").hasClass("sample1")) {
console.log("divid has sample1 : ", isHas);
$("#divid1").removeClass("sample1");
}
});
</script>