JavaScript DHTML/jQuery/Selector first last

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

firstChild(): One for each parent.

   <source lang="html4strict">
 

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
               $("div span:first-child")
               .css("text-decoration", "underline")
               .hover(function () {
                     $(this).addClass("red");
                   }, function () {
                     $(this).removeClass("red");
                   });
       });
   </script>
   <style>
   
     span { color:#008; }
     span.red { color:red; font-weight: bolder; }
     
   </style>
 </head>
 <body>
   <body>
         A,
         B,
         C
         D,
         E,
         F
   </body>

</html>


 </source>
   
  


first() matches only a single element

   <source lang="html4strict">
 

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
               
           $("tr:first").css("background-color", "red");
               
       });
   </script>
 </head>
 <body>
   <body>
TD #0TD #1TD #2
TD #3TD #4TD #5
TD #6TD #7TD #8
   </body>

</html>


 </source>
   
  


Get first child ID

   <source lang="html4strict">
    

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
          $("#container").click(function (e) {
             var $ch = $(e.target).children();
             
             $("#results span:first").text($ch.length);
             
             e.preventDefault();
             return false;
           });
       });
   </script>
 </head>
 <body>
   <body>

This is the way we write the demo,

   Found 0 children in TAG.
 </body>

</html>



 </source>
   
  


Get first paragraph

   <source lang="html4strict">
    

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
              var str = $("p:first").text()+"added";
              $("p:last").html(str);
       });
   </script>
 </head>
 <body>
   <body>

asdf

asdf

asdf

asdf

 </body>

</html>



 </source>
   
  


Get last paragraph

   <source lang="html4strict">
     

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
              var str = $("p:first").text()+"added";
              $("p:last").html(str);
       });
   </script>
 </head>
 <body>
   <body>

asdf

asdf

asdf

asdf

 </body>

</html>




 </source>
   
  


lastChild() : One for each parent.

   <source lang="html4strict">
 

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
               $("div span:last-child")
               .css("text-decoration", "underline")
               .hover(function () {
                     $(this).addClass("red");
                   }, function () {
                     $(this).removeClass("red");
                   });
       });
   </script>
   <style>
   
     span { color:#008; }
     span.red { color:red; font-weight: bolder; }
     
   </style>
 </head>
 <body>
   <body>
         A,
         B,
         C
         D,
         E,
         F
   </body>

</html>


 </source>
   
  


last() matches the last selected element.

   <source lang="html4strict">
 

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
          $("tr:last").css({backgroundColor: "yellow", fontWeight: "bolder"});
       });
   </script>
 </head>
 <body>
   <body>
First
Middle
Last
   </body>

</html>


 </source>
   
  


Matches the first selected element

   <source lang="html4strict">
     

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
 $(document).ready(function(){
     $("tr:first").css("font-style", "italic");
 });
   </script>
 </head>
 <body>
Row 1
Row 2
Row 3
 </body>

</html>




 </source>
   
  


Matches the last selected element

   <source lang="html4strict">
     

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
 $(document).ready(function(){
     $("tr:last").css("font-style", "italic");
 });
   </script>
 </head>
 <body>
Row 1
Row 2
Row 3
 </body>

</html>




 </source>
   
  


Select first span

   <source lang="html4strict">
    

<html>

 <head>
   <style>
     .test{ border: 1px solid red; }
   </style>
 
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
   $(document).ready(function(){
          $("span:first").text($(":hidden", document.body).length + " hidden elements.");
   });
   </script>
 </head>
 <body>
     
Hider!
       <form>
           <input type="hidden" />
           <input type="hidden" />
           <input type="hidden" />
       </form>
       
 </body>

</html>




 </source>