JavaScript Tutorial/jQuery/get

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

Get first element in query set

   <source lang="javascript">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
          var mappedItems = $("li").map(function (index) {
var data = $("
  • ").text($(this).text()).get(0); data = [data,$("
  • ").get(0)]; $(data[0]).append("*"); return data; }); $("#results").append(mappedItems); }); </script> </head> <body> <body>
    • asdf
    • asdf
    • asdf
     </body>
    

    </html></source>


    Get tag first parent

       <source lang="javascript">
    

    <html>

     <head>
       <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
       <script type="text/javascript">
           $(document).ready(function(){
                $("*", document.body).each(function () {
                     var parentTag = $(this).parent().get(0).tagName;
                     alert(parentTag);
                     
                });
           });
       </script>
       <style>
         .y { background:yellow; }
     </style>
     </head>
     <body>
       <body>
    
    <button disabled="disabled">First</button>
               
    
     </body>
    

    </html></source>


    Gives the tag name of the element clicked on.

       <source lang="javascript">
    

    <html>

     <head>
       <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
       <script type="text/javascript">
           $(document).ready(function(){
               $("*", document.body).click(function (e) {
                 e.stopPropagation();
                 var domEl = $(this).get(0);
                 $("span:first").text("Clicked on - " + domEl.tagName);
               });
           });
       </script>
     </head>
     <body>
       <body>
           
    

    A

    B
       </body>
    

    </html></source>


    Uses the built-in reverse-method to reverse array

       <source lang="javascript">
    

    <html>

     <head>
       <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
       <script type="text/javascript">
           $(document).ready(function(){
                   
                  alert( $("p").get().reverse() );
           });
       </script>
     </head>
     <body>
       <body>
    

    A

    B

       </body>
    
    </html></source>