我有不同类别名称的幻灯片’左”’右”’在底部’等.我需要首先找到任何一个以’in-‘开头的类的幻灯片,但是我需要将完整的类名’in-left’用于switch语句.我试图找到最有效的方法来
通常我会使用数据属性data-in =“left”,但不能用于此项目.
<div class="container">
<div class="slide in-left"></div>
<div class="slide in-right"></div>
<div class="slide in-bottom"></div>
<div class="slide in-top"></div>
</div>
jQuery的
var $els = $('.slide[class*="in-"]');
$.each( $els, function(k, v){
var type = v; // Get full class name for classes that start with 'in-'
switch (type) {
case 'in-left':
// Transition in from left
break;
case 'in-bottom':
// Transition in from bottom
break;
}
});
如果你知道可能的值,使用
hasClass怎么样?
$('.slide').each( function(){
var $this = $(this);
if ($this.hasClass('in-left')) {
} else if ($this.hasClass('in-bottom')) {
} /* else if() ... */
});
