【问题标题】:jQuery throwing up "Unexpected token for" as an errorjQuery抛出“Unexpected token for”作为错误
【发布时间】:2015-04-26 03:26:30
【问题描述】:

我完全不知道问题出在哪里,但下面的 for 循环会抛出“Unexpected token for”。

删除 for 循环会导致关闭 };被称为意外令牌。

$.fn.appExt = function() {

        $(data).find('a').each(function() {

                fileExt = this.href.replace(window.location, '').replace('localhost/Program/Code', '').split('.')[1];

                if ($.inArray(fileExt, ext) == -1 && typeof fileExt !== 'undefined') {
                    ext.push(fileExt);
                }

            }

            // here lies the problem apparently
            for (i = 0; i < ext.length; i++) {
                $('#ext').append('<h5>' + ext[i] + '</h5>');
            }

        };

【问题讨论】:

    标签: jquery loops for-loop token


    【解决方案1】:

    您需要添加); 来关闭对.each() 的调用,但您也可以添加一些var 关键字,这样您就不会创建全局变量:

    $.fn.appExt = function () {
        $(data).find('a').each(function () {
            var fileExt = this.href.replace(window.location,
                '').replace('localhost/Program/Code', '').split('.')[1]; // [1] Added "var"
    
            if ($.inArray(fileExt, ext) == -1 && typeof fileExt !== 'undefined') {
                ext.push(fileExt);
            }
        }); // [2] Added ");"
    
        for (var i = 0; i < ext.length; i++) { // [3] Added "var"
            $('#ext').append('<h5>' + ext[i] + '</h5>');
        }
    };
    

    你应该整齐地缩进你的代码,这样意图就更清楚了。

    【讨论】:

      【解决方案2】:

      您还没有关闭每个函数的括号。

      【讨论】:

        【解决方案3】:

        您的.each 函数如下所示:

        $(data).find('a').each(function() { };
        

        这意味着您错过了.each( 的右括号

        当你开始一个标签时,首先要做的应该是在你在标签中放东西之前添加它的结束标签:

         $(data).find('a').each(function() { 
               //ready to add the code
         });
        

        【讨论】:

          【解决方案4】:

          您没有关闭 .each 函数括号。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-13
            • 2018-03-08
            • 1970-01-01
            • 2012-04-23
            • 2021-06-23
            • 2017-04-02
            相关资源
            最近更新 更多