JavaScript Tutorial/jQuery/find

Материал из Web эксперт
Перейти к: навигация, поиск

Change the font weight and color of all Italic text

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <style type="text/css">
        a.test { font-weight: bold; color:red;}
    </style>
    <script type="text/javascript">
 $(document).ready(function(){
    $("input.buttonA").click(function(){ $("div.contentToChange").find("em").css({color:"#993300", fontWeight:"bold"}); });

 });
    </script>
  </head>
  <body>
    <input type="button" value="Change" class="buttonA" />

    <div style="background:#eee;" class="contentToChange">
        <h2>Header 2</h2>
       
        <p class="firstparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="fifthparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
    </div>
  </body>
</html>


Find all children with a class "selected" of each div.

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
               $("div").children(".selected").css("color", "blue");
                    
        });
    </script>
  </head>
  <body>
    <body>
        <div id="container">
            <div class="selected">And Again</div>
            <div>
              asdf
            </div>
        </div>
          
    </body>
</html>


find(expr): searching is done using a jQuery expression

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
         $("p").find("span").css("color","red");
                    
        });
    </script>
  </head>
  <body>
    <body>
        
        <p>bad or <span>good</span>.</p>
          
    </body>
</html>


Find tag in another tag

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").find("span").css("color","red");

        });
    </script>
  </head>
  <body>
    <body>
       <p><span>Hello</span></p>

  </body>
</html>