2017-04-18 346 views
0

我使用jquery插件來顯示滑塊網格,當我使用jquery 1.6時它工作正常,當我更改爲jquery 2.4。 1它顯示這樣的錯誤:未捕獲的錯誤:語法錯誤,無法識別的表達式:.tj_row_1,.tj_row_2,.tj_row_3,在Function.Sizzle.error

未被捕獲的錯誤:語法錯誤,不能識別的表達式:.tj_row_1,.tj_row_2,.tj_row_3,在Function.Sizzle.error

搜索後,我發現造成這部分代碼的錯誤:

pagination : function($wrapper, dir, opts) { 
        var config = $wrapper.data('config'); 

        if((dir === 1 && config.currentRow + opts.rows > config.totalRows) || 
         (dir === -1 && config.currentRow - 1 <= 0) 
        ) { 
         $wrapper.data('anim', false); 
         return false; 
        } 

        var movingRows = ''; 

        for(var i = 0; i <= opts.rows; ++i) { 
         (dir === 1) 
          ? movingRows += ".tj_row_" + (config.currentRow + i) + "," 
          : movingRows += ".tj_row_" + (config.currentRow + (i - 1)) + ","; 
        } 

        var seq_t = opts.type.factor, 
         $elements; 

        var dircond = 1; 
        if(opts.type.reverse) dircond = -1; 
        if(dir === dircond) { 
         $elements = $wrapper.children(movingRows); 
        }else { 
         $elements = $wrapper.children(movingRows).reverse(); 
        } 
        var total_elems = $elements.length, 
         cnt   = 0; 

        $elements.each(function(i) { 
         var $el   = $(this), 
          row   = $el.attr("class"), 
          animParam = {}, 

          currentRow = config.currentRow; 

         setTimeout(function() { 
// if first row fade out 
// if last row fade in 
// for all the rows move them up/down 
          if(dir === 1) { 
           if( row === "tj_row_" + (currentRow)) { 
            animParam.opacity = 0; 
           } 
           else if(row === "tj_row_" + (currentRow + opts.rows)) { 
            animParam.opacity = 1; 
           } 
          } 
          else { 
           if( row === "tj_row_" + (currentRow - 1)) { 
            animParam.opacity = 1; 
           } 
           else if(row === "tj_row_" + (currentRow + opts.rows - 1)) { 
            animParam.opacity = 0; 
           } 
          } 

          $el.show(); 

          (dir === 1) 
           ? animParam.top = $el.position().top - $el.height() + 'px' 
           : animParam.top = $el.position().top + $el.height() + 'px' 

          $el.stop().animate(animParam, opts.type.speed, opts.type.easing, function() { 
           if(parseInt(animParam.top) < 0 || parseInt(animParam.top) > $el.height() * (opts.rows - 1)) 
            $el.hide(); 

           ++cnt; 
           if(cnt === total_elems) { 
            $wrapper.data('anim', false); 
           } 
          }); 
         }, seq_t + i * seq_t); 
        }); 

        (dir === 1) ? config.currentRow += 1 : config.currentRow -= 1; 

        $wrapper.data('config', config); 
       } 

回答

0

該問題是由於尾隨,上的movingRows選擇器字符串。

在使用前解決這個問題,你會更好地創建一個數組中的選擇,然後join()他們,這樣的事:

var config = { currentRow: 1 } 
 
var opts = { rows: 5 } 
 
var dir = 1; 
 

 
var classes = []; 
 
for (var i = 0; i < opts.rows; i++) { 
 
    var suffix = config.currentRow + (dir == 1 ? i : i - 1); 
 
    classes.push('.tj_row_' + suffix); 
 
} 
 

 
var movingRows = classes.join(','); 
 
console.log(movingRows);

+0

錯誤已更改爲:遺漏的類型錯誤在HTMLSpanElement上的jquery.gridnav.js:345) 。 (jquery.gridnav.js:710) at HTMLSpanElement.dispatch(jquery-2.1.4.js:4435) at HTMLSpanElement.elemData.handle(jquery-2.1.4.js:4121) –

+0

啊是的,我的錯誤。我以爲'opts.rows'是一個數組。我爲你更新了答案 –

0

的問題是在這裏傳遞的價值:

(dir === 1)? $currentRowElements = $wrapper.children(currentRows) 
: $currentRowElements = $wrapper.children(currentRows).reverse(); 

的currentRows = 「的值串」= tj_row_1,.tj_row_2,.tj_row_3,.tj_row_4,.T j_row_5

我的字符串轉換爲數組,然後我得到數組的下一個值:在Object.pagination opts.rows.map不是一個函數 (:

 pagination : function($wrapper, dir, opts) { 
         var config = $wrapper.data('config'); 

         if((dir === 1 && config.currentRow + opts.rows > config.totalRows) || 
          (dir === -1 && config.currentRow - 1 <= 0) 
         ) { 
          $wrapper.data('anim', false); 
          return false; 
         } 

         var movingRows = ''; 

         for(var i = 0; i <= opts.rows; ++i) { 
          (dir === 1) 
           ? movingRows += '.tj_row_' + (config.currentRow + i) + ',' 
           : movingRows += '.tj_row_' + (config.currentRow + (i - 1)) + ','; 
         } 
         //var movingRows = opts.rows.map(function(v, i) { 
         // var suffix = config.currentRow + (dir == 1 ? i : i - 1); 
         // return '.tj_row_' + suffix; 
         //}).join(','); 

         var seq_t = opts.type.factor, 
          $elements; 

         var dircond = 1; 
         if(opts.type.reverse) dircond = -1; 
//convert to array 
         arr = movingRows.split(','); 
//function to get the next value of array 
         function nextSlide(num) { 
          var p=[]; 
          return p[($.inArray(num, p) + 1) % p.length]; 
         } 
         (dir === dircond) 
          ? $elements = $wrapper.children(nextSlide(arr)) 
          : $elements = $wrapper.children(nextSlide(arr)).reverse(); 

         var total_elems = $elements.length, 
          cnt   = 0; 

         $elements.each(function(i) { 
          var $el   = $(this), 
           row   = $el.attr('class'), 
           animParam = {}, 

           currentRow = config.currentRow; 

          setTimeout(function() { 
           // if first row fade out 
           // if last row fade in 
           // for all the rows move them up/down 
           if(dir === 1) { 
            if( row === 'tj_row_' + (currentRow)) { 
             animParam.opacity = 0; 
            } 
            else if(row === 'tj_row_' + (currentRow + opts.rows)) { 
             animParam.opacity = 1; 
            } 
           } 
           else { 
            if( row === 'tj_row_' + (currentRow - 1)) { 
             animParam.opacity = 1; 
            } 
            else if(row === 'tj_row_' + (currentRow + opts.rows - 1)) { 
             animParam.opacity = 0; 
            } 
           } 

           $el.show(); 

           (dir === 1) 
            ? animParam.top = $el.position().top - $el.height() + 'px' 
            : animParam.top = $el.position().top + $el.height() + 'px' 

           $el.stop().animate(animParam, opts.type.speed, opts.type.easing, function() { 
            if(parseInt(animParam.top) < 0 || parseInt(animParam.top) > $el.height() * (opts.rows - 1)) 
             $el.hide(); 

            ++cnt; 
            if(cnt === total_elems) { 
             $wrapper.data('anim', false); 
            } 
           }); 
          }, seq_t + i * seq_t); 
         }); 

         (dir === 1) ? config.currentRow += 1 : config.currentRow -= 1; 

         $wrapper.data('config', config); 
        } 
相關問題