Attributs: fonction hasClass

Retourne vrai si la classe spécifiée est présente pour au moins un des éléments ciblés. Equivaut à la fonction is(".maClasse").

Version: disponible depuis la version 1.0 de JQuery

Paramétres:

  • classe (string): nom de la classe CSS

Valeur de retour: ut

Exemple :

si l'élement a la classe "protected", alors lui associe une animation au clic.

$("div").click(function(){
      if ( $(this).hasClass("protected") )
        $(this).animate({ left: -10 }, 75)
               .animate({ left: 10 }, 75)
               .animate({ left: -10 }, 75)
               .animate({ left: 10 }, 75)
               .animate({ left: 0 }, 75);
    });
  <html>
  <head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  <script type="text/javascript">
  	$(document).ready(function(){
  		$("div").click(function(){
      if ( $(this).hasClass("protected") )
        $(this).animate({ left: -10 }, 75)
               .animate({ left: 10 }, 75)
               .animate({ left: -10 }, 75)
               .animate({ left: 10 }, 75)
               .animate({ left: 0 }, 75);
    });

  	});
  </script>
  
  <style>
  
  div { width: 80px; height: 80px; background: #abc; 
        position: relative; border: 2px solid black; 
        margin: 20px 0px; float: left; left:0 }
  div.protected { border-color: red; }
  span { display:block; float:left; width:20px; 
         height:20px; }
  
  </style>
  </head>
  <body>
  	
  <span></span><div class="protected"></div>
  <span></span><div></div>
  <span></span><div></div>
  <span></span><div class="protected"></div>


  </body>
  </html>

0 Commentaire (afficher/poster)