Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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
jQuery方法&x27;hasClass';在FF中工作良好,但在镀铬中不起作用 我正在使用基础框架并使用轨道滑块,我创建了一个方法来获得幻灯片放映的实际显示幻灯片索引。在firefox中,一切正常,函数返回正确的实际幻灯片索引,但在Chrome中,它返回“-1”,尽管HTML代码包含请求的元素 // function for getting the actual slide index of a slide show function getActualSlideIndex(){ $counter = 0; $( '#featured1 > li' ).each(function (){ console.log($( this )); if ($(this).hasClass('active')){ return $counter; } $counter++; }); return -1; }_Jquery - Fatal编程技术网

jQuery方法&x27;hasClass';在FF中工作良好,但在镀铬中不起作用 我正在使用基础框架并使用轨道滑块,我创建了一个方法来获得幻灯片放映的实际显示幻灯片索引。在firefox中,一切正常,函数返回正确的实际幻灯片索引,但在Chrome中,它返回“-1”,尽管HTML代码包含请求的元素 // function for getting the actual slide index of a slide show function getActualSlideIndex(){ $counter = 0; $( '#featured1 > li' ).each(function (){ console.log($( this )); if ($(this).hasClass('active')){ return $counter; } $counter++; }); return -1; }

jQuery方法&x27;hasClass';在FF中工作良好,但在镀铬中不起作用 我正在使用基础框架并使用轨道滑块,我创建了一个方法来获得幻灯片放映的实际显示幻灯片索引。在firefox中,一切正常,函数返回正确的实际幻灯片索引,但在Chrome中,它返回“-1”,尽管HTML代码包含请求的元素 // function for getting the actual slide index of a slide show function getActualSlideIndex(){ $counter = 0; $( '#featured1 > li' ).each(function (){ console.log($( this )); if ($(this).hasClass('active')){ return $counter; } $counter++; }); return -1; },jquery,Jquery,return$counter从传递给每个的内部回调返回,而不是从外部函数返回。你可能想要这个: // function for getting the actual slide index of a slide show function getActualSlideIndex(){ $counter = 0; $( '#featured1 > li' ).each(function (){ if ($(this).hasClass('act

return$counter
从传递给每个
的内部回调返回,而不是从外部函数返回。你可能想要这个:

// function for getting the actual slide index of a slide show
function getActualSlideIndex(){
      $counter = 0;
       $( '#featured1 > li' ).each(function (){
           if ($(this).hasClass('active')){
               return false;
           }
          $counter++; 
       });
      return $counter || -1;
}

可以简化为

function getActualSlideIndex() {
    var index = $('#featured1 > li.active').index();
    return index == -1 ? -1 : index + 1;
}

演示:

请在jsfiddle上复制它,我非常怀疑它在Firefox中是否返回了正确的索引。在任何浏览器上,它都应始终返回-1。请你修改一下相关的代码好吗?正确后返回false停止所有操作?我认为只需返回就可以退出函数。
.each()
中的“return false”没有意义,如果这样做,最好优化代码。函数始终会退出。返回false会阻止下一次迭代(从而防止计数器递增)。