웹곡뷰

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>