【问题标题】:"Unexpected token else" inside $.each()$.each() 中的“其他意外令牌”
【发布时间】:2013-06-03 00:35:06
【问题描述】:

由于某种原因,我收到此脚本的 Unexpected token else 错误:

.each(rowValue.columns, function(columnKey, columnValue){
    if(typeof rowValue.type !== 'undefined' && rowValue.type == 'header'){
        $(this).append(
            $('<div></div>').prop('name', columnValue.name).text(columnValue.text).addClass('orangeStatusTwoThirdsCell blueCellOverride').applyHTMLclasses(columnValue.classes);
        );
    }
    else if(columnValue.inputType == 'textarea'){
        $(this).append(
            $('<textarea></textarea>').prop('rows', columnValue.textareaRows).prop('id', columnValue.id).addClass('fillContainer inputAsCell hideScrollbar').applyHTMLclasses(columnValue.classes);
        );
    }
    else if(columnValue.inputType == 'select'){
        $(this).append(
            $('<select></select>').prop('id', columnValue.id).addClass('fillContainer inputAsCell').applyHTMLclasses(columnValue.classes).append(function(){
                if(typeof columnValue.valuelessSelectOption !== 'undefined'){$(this).append($('<option/>').text(columnValue.valuelessSelectOption));}
            });
        );
    }
});

$.append()ing 这些动态构造的元素。

【问题讨论】:

  • 您还有其他错误。
  • @dystroy 你介意给我看看吗?蒂亚!
  • 是错字还是你在.each()之前忘记了$
  • 行数较短的代码通常更容易被人类解析。以if(typeof columnValu 开头的行特别难以阅读。我不会检查它,因为我讨厌必须重复使用水平滚动条。
  • @Karl-AndréGagnon 在此之前还有很多。只包括相关部分

标签: javascript jquery append syntax-error each


【解决方案1】:

删除 ;在这中间行的末尾:

if(typeof rowValue.type !== 'undefined' && rowValue.type == 'header'){
    $(this).append(
        $('<div></div>').prop('name', columnValue.name).text(columnValue.text).addClass('orangeStatusTwoThirdsCell blueCellOverride').applyHTMLclasses(columnValue.classes);
    );
}

几行之后你也遇到了同样的问题。

【讨论】:

  • 这个;是把表达式改成语句,不能作为append函数的参数。
猜你喜欢
  • 2020-12-04
  • 2017-05-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 2021-03-19
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多