JavaScript DHTML/jQuery/siblings

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

Add style to siblings

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">

var tmpExample = {

 ready : function() {
   $("ul#tmpPlaces li.myStyle").siblings().addClass("tmpSiblings");
 }

}; $(document).ready(tmpExample.ready);

   </script>
   <style type="text/css">

body {

   font: 16px sans-serif;

} h4 {

   font-size: 16px;
   margin: 0 0 5px 0;

} ul#tmpPlaces {

   list-style: none;
   margin: 0;
   padding: 0;

} ul#tmpPlaces li {

   margin: 1px;
   padding: 3px;

} li.tmpSiblings{

   background: khaki;

}

   </style>
 </head>
 <body>
  • a
  • b
  • c
  • d
  • e
  • f
  • g
 </body>

</html>

 </source>
   
  


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

   <source lang="html4strict">
 

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
           
              $("p").siblings(".selected").css("background", "yellow");
               
        
       });
   </script>
   <style>
     .selected { color:blue; }
     
   </style>
   
 </head>
 <body>
   <body>
         asdf</div>

asdf asdf

asdf

   </body>

</html>


 </source>
   
  


Get siblings

   <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").siblings().css("background", "yellow");
       });
   </script>
 </head>
 <body>
   <body>
asdf
asdf
asdf
asdf
asdf
 </body>

</html>



 </source>
   
  


Sibling by class name

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">

var tmpExample = {

 ready : function() {
   $("ul#uiID li.liClassName3").siblings(".liClassName").addClass("mySiblings");
 }

}; $(document).ready(tmpExample.ready);

   </script>
   <style type="text/css">

ul#uiID {

   list-style: none;
   margin: 0;
   padding: 0;

} ul#uiID li {

   margin: 1px;
   padding: 3px;

} li.mySiblings{

   background: khaki;

}

   </style>
 </head>
 <body>
  • A
  • B
  • C
  • D
  • E
  • F
  • G
 </body>

</html>

 </source>