본문 바로가기

강좌/jQuery

제이쿼리(jQuery) Traversing



1. eq메서드

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>

<meta http-equiv="content-type" content="text/html;" charset="utf-8" >

        <title> jquery </title>

<script src="http://code.jquery.com/jquery-1.7.2.js"></script>

<style type="text/css">

            .red{background-color:red;}

</style>

<script>

   $(document).ready(function (){

   alert($('p').html()); //한개만 읽음C#

   alert($('p').text());

alert($('p:eq(2)').text());

alert($('p:eq(2)').html());

});

</script>

    </head>


    <body>

<p>C#</p>

<p>ASP.NET</p>

<p>jQuery</p>

    </body>

</html>


2. is () 메소드

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8" >
        <title> jquery </title>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script>
   $(document).ready(function (){
                            //myForm영역에 submit버튼이 있는지 검사
   if($('#myForm').children().is("input[type=submit]")){
alert("yes");
}else{
alert("no");
}
});
</script>
    </head>

    <body>
<div id="myForm">
   <input type="text" />
</div>
    </body>
</html>

3. 지정된 표현식을 제외한 나머지에 함수 적용하기

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8" >
        <title> jquery </title>
<style>
   .yellow{background-color:yellow;}

</style>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script>
   $(document).ready(function (){
   //라디오버튼을 제외한 input태그에 배경색을 yellow로 설정
$('input').not("input[type=radio]").each(function(){
   $(this).addClass("yellow").css("border","1px solid red");
});
});
</script>
    </head>

    <body>
<div id="myForm">
   <input type="text" />
<input type="password" />
<input type="radio" />
</div>
    </body>
</html>

4. 방금 실행한 집합 이전으로 되돌리기
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8" >
        <title> jquery </title>
<style>
   .yellow{background-color:yellow;}

</style>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script>
   $(document).ready(function (){
   //javascript 텍스트 찾아가지1
alert($('div').find('p').html());
   //javascript 텍스트 찾아가지1
var result = $('div').find('p:last').find('b').html();
alert(result);
//방금 탐색한 기능 이전으로 돌리기
var html = $('div').find('p:last').find('b').end().html();
alert(html); //jQuery->end() -><b>jQuery</b>

});
</script>
    </head>

    <body>
<div id="myForm">
  <p><b>javascript</b></p>
  <p><b>jquery</b></p>
</div>
    </body>
</html>