Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery-使用each()获取类元素的位置_Javascript_Jquery_Each - Fatal编程技术网

Javascript Jquery-使用each()获取类元素的位置

Javascript Jquery-使用each()获取类元素的位置,javascript,jquery,each,Javascript,Jquery,Each,我需要知道“猫头鹰物品激活”的位置,我知道当前位置是2。总共有3张照片 <div style="transform: translate3d(-825px, 0px, 0px); transition: all 0.25s ease 0s; width: 48675px;" class="owl-stage"> <div style="width: 825px; margin-right: 0px;" class="owl-item"> <d

我需要知道“猫头鹰物品激活”的位置,我知道当前位置是2。总共有3张照片

<div style="transform: translate3d(-825px, 0px, 0px); transition: all 0.25s ease 0s; width: 48675px;" class="owl-stage">
    <div style="width: 825px; margin-right: 0px;" class="owl-item">
        <div class="gallery-item" style="background-image: url('/image/144/1.jpg');"></div>
    </div>
    <div style="width: 825px; margin-right: 0px;" class="owl-item active">
        <div class="gallery-item" style="background-image: url('/image/144/2.jpg');"></div>
    </div>
    <div style="width: 825px; margin-right: 0px;" class="owl-item">
        <div class="gallery-item" style="background-image: url('/image/144/3.jpg');"></div>
    </div>
</div>
有人能告诉我怎么做吗?

您需要使用以下方法:

$('.owl-stage .owl-item').each(function( index, currentElement ) {
   if( $( this ).hasClass( 'active' ) ) { 
     console.log( index+1 );
   }
});
但只需将选择器传递给方法,就可以更轻松地使用它:

$(函数(){
log('.activeindex为',$('.owl stage.active')。index('.owl item')+1);
});

$('.owl-stage .owl-item').each(function( index, currentElement ) {
   if( $( this ).hasClass( 'active' ) ) { 
     console.log( index+1 );
   }
});
console.log( $('.owl-stage .active').index( '.owl-item' ) + 1 );