Javascript Jquery错误-未捕获错误:语法错误,无法识别的表达式

Javascript Jquery错误-未捕获错误:语法错误,无法识别的表达式,javascript,jquery,Javascript,Jquery,我正在用Wordpress创建一个网站,在一个自定义的帖子类型中,我使用了一个稍微不同的输入界面,我认为这是合乎逻辑的: 列出类别(分类法) 列出该类别的帖子(频道) 如果您单击一篇文章,此文章将添加到文本字段,如果再次单击,它将被删除 代码有时工作正常,但神奇地停止: load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3 未捕获错误:语法错误,无法识别的表达式:div div.cha

我正在用Wordpress创建一个网站,在一个自定义的帖子类型中,我使用了一个稍微不同的输入界面,我认为这是合乎逻辑的:

  • 列出类别(分类法)
  • 列出该类别的帖子(频道)
  • 如果您单击一篇文章,此文章将添加到文本字段,如果再次单击,它将被删除
代码有时工作正常,但神奇地停止:

load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3 未捕获错误:语法错误,无法识别的表达式:div div.channel 在Function.fa.error(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3) 在fa.tokenize(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3) 在fa.select(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3) 在Function.fa(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3) 在Function.a.find(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:10) 在n.fn.init.find(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3) 在n.fn.init.a.fn.find(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:10) 在a.fn.init.n.fn.init(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3) 在新的a.fn.init(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:10) 在n处(load scripts.php?c=0&load[]=utils,jquery核心,jquery迁移,moxiejs,plupload&ver=4.9.8:3)

我的代码:

        <script>
        // Carrega as categorias de canais
        jQuery.get(
            'http://meusite.com/wordpress/wp-json/wp/v2/canais-categorias?per_page=100&orderby=name&order=asc',
            function(data){
                for(var i=0; i < data.length; i++){
                    jQuery('div#channels').append(
                        '<div class="row">'                                                                                     +
                            '<div class="col-12">'                                                                          +
                                '<h3>' + data[i].name + '</h3>'                                                     +
                            '</div>'                                                                                                        +
                        '</div>'                                                                                                            +
                        '<div class="row channel-grid" id="' +  data[i].slug + '">'     +
                        '</div>'
                    );
                }
            }, 'json'
        );

        // Carrega os canais
        jQuery.get(
            'http://meusite.com/wordpress/wp-json/wp/v2/canais?per_page=100&orderby=title&order=asc',
            function(data){
                for(var i=0; i<data.length; i++){
                    jQuery('div#' + data[i].categoria[0]).append(
                        '<div class="col-3" id="' + data[i].slug + '">'         +
                            '<div class="channel">'                                                     +
                                '<img src="' + data[i].channel_image + '">'         +
                                '<p>' + data[i].title.rendered + '</p>'                 +
                            '</div>'                                                                                    +
                        '</div>'
                    );
                }

                // Seleciona os caais ativos
                var channel_grid = jQuery('input#plan_channels_grid').val();
                var channel_grid = channel_grid.substring(0, channel_grid.length-1);
                var channel_grid = channel_grid.split(',');

                for(var i=0; i<channel_grid.length; i++){
                    jQuery('div#' + channel_grid[i] + ' div.channel').addClass('active');
                }

                // Adiciona e remove um canal com um clique
                jQuery('div.channel').click(function(){
                    var id = jQuery(this).parent().attr('id');

                    if(jQuery('div#' + id + ' div.channel').hasClass('active')){
                        jQuery('div#' + id + ' div.channel').removeClass('active');

                        var ch = jQuery('input#plan_channels_grid').val();
                        jQuery('input#plan_channels_grid').val(ch.replace((id + ','), ''));
                    }else{
                        jQuery('div#' + id + ' div.channel').addClass('active');

                        var ch = jQuery('input#plan_channels_grid').val();
                        jQuery('input#plan_channels_grid').val(ch + id + ',');
                    }
                });
            }, 'json'
        );
    </script>

//卡雷加被归类为加拿斯
jQuery.get(
'http://meusite.com/wordpress/wp-json/wp/v2/canais-categorias?per_page=100&orderby=name&order=asc',
功能(数据){
对于(变量i=0;idiv#div.channel
是一个无效的css选择器。
#
后面有一个空格,这将成为一个无效的ID选择器。因此,您的一个字符串串联要么是将字符串与前导空格串联,要么是将一个空变量串联

例如:

jQuery('div#' + id + ' div.channel')
如果
id
是一个空字符串,您将得到错误提到的字符串。与以下相同:

jQuery('div#' + channel_grid[i] + ' div.channel')

如果
通道网格[i]
为空将导致无效的css选择器。请检查您的代码,确保您的代码设置正确以生成这些变量,并且没有生成空字符串。或者在尝试使用它们之前使用错误检查。

Patrick,您的答案给我答案!我的代码在新帖子中不起作用,因为我没有值在字段中进行拆分并得到“channel_grid”。用一个简单的“if(channel_grid>0){”我解决了这个问题!谢谢!