[Функция] hasClass(class)
Раздел "Traversing"
Функция проверяет текущий выбор элементов на наличие определенного класса и возвращает true, если, поменьшей мере, имеется одни элемент с искомым классом.
Параметры
hasClass(class)
1.2
class:искомый класс.
Описание
Данная функция является альтернативой для ("." + class).
Примеры
Пример:
Проверить присутсвует ли обозначенный класс у элемента, если класс присутствует, выполнить дейтсвие.
"jQuery"
$("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"
<span></span><div class="protected"></div> <span></span><div></div> <span></span><div></div> <span></span><div class="protected"></div>
"CSS"
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; }
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <link href="http://test-drupal.ru/themes/slyweb/css/jqueryiframe.css" rel="stylesheet" type="text/css"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script> $(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> </head> <body class="iframe"> <span></span><div class="protected"></div> <span></span><div></div> <span></span><div></div> <span></span><div class="protected"></div> </body> </html> <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>
Версия jQuery 1.4.2
Документ создан 2010-08-21