未捕获的TypeError:无法读取属性' 1'

时间:2014-03-14 16:00:44

标签: javascript

我遇到的问题只出现在chrome中,并且在firefox中运行正常,没有任何问题

      computedStyle = getComputedStyle(element, ':before');
      content = computedStyle.getPropertyValue('content').slice(1, -1);
      // content --> NULL
      matchResult = content.match(/^\s*(\d+)(?:\s?\.(.+))?\s*$/);
      if (matchResult) {
        numberOfColumns = matchResult[1];
        columnClasses = ((_ref = matchResult[2]) != null ? _ref.split('.') : void 0) || ['column'];
      } else {
        matchResult = content.match(/^\s*\.(.+)\s+(\d+)\s*$/);
        columnClasses = matchResult[1];    // <-- Error here
        numberOfColumns = (_ref1 = matchResult[2]) != null ? _ref1.split('.') : void 0;
      }
      return {
        numberOfColumns: numberOfColumns,
        columnClasses: columnClasses
      };
    };
    add_columns = function(grid, items) {
      var columnClasses, columnsFragment, columnsItems, i, 

并且控制台中的错误是Uncaught TypeError: Cannot read property '1' of null

1 个答案:

答案 0 :(得分:0)

// ...
if (matchResult) {
    numberOfColumns = matchResult[1];
    columnClasses = ((_ref = matchResult[2]) != null ? _ref.split('.') : void 0) || ['column'];
} else if (matchResult = content.match(/^\s*\.(.+)\s+(\d+)\s*$/)){
    columnClasses = matchResult[1];    // <-- Error here
    numberOfColumns = (_ref1 = matchResult[2]) != null ? _ref1.split('.') : void 0;
} else {
    columnClasses = numberOfColumns = null;
}
// ...

在处理数据之前,请确保matchResult不为空。