简体   繁体   English

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

[英]Jquery error - Uncaught Error: Syntax error, unrecognized expression

I'm creating a website in Wordpress, and in a Custom Post Type with a little different input interface I thought this logical:我在 Wordpress 中创建了一个网站,并在一个输入界面略有不同的自定义帖子类型中,我认为这是合乎逻辑的:

  • List categories (taxonomies);列出类别(分类法);
  • List posts (channels) from this categories;列出此类别的帖子(频道);
  • If you click on a post, this post is added to a text field, if click again, it's removed;如果你点击一个帖子,这个帖子会被添加到一个文本字段中,如果再次点击,它会被删除;

The code works fine some time, but magically stops:该代码在一段时间内运行良好,但神奇地停止了:

load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3 Uncaught Error: Syntax error, unrecognized expression: div# div.channel at Function.fa.error (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) at fa.tokenize (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) at fa.select (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) at Function.fa (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) at Function.a.find (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:10) at n.fn.init.find (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) at n.fn.init.a.fn.find (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:10) at a.fn.init.n.fn.init (load-scripts.php?c=0&load[]=utils,jquery-cor load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3未捕获错误:语法错误,无法识别的表达式: Function.fa 中的div# div.channel 。错误 (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) 在 fa.tokenize (load-scripts.php?c=0&load[] =utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) 在 fa.select (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs, plupload&ver=4.9.8:3) 在 Function.fa (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) 在 Function.a。在 n.fn.init.find (load-scripts.php?c) 处找到 (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:10) =0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) 在 n.fn.init.a.fn.find (load-scripts.php?c=0&load[]= utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:10) at a.fn.init.n.fn.init (load-scripts.php?c=0&load[]=utils,jquery-cor e,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) at new a.fn.init (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:10) at n (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) e,jquery-migrate,moxiejs,plupload&ver=4.9.8:3) 在新的 a.fn.init (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver= 4.9.8:10) 在 n (load-scripts.php?c=0&load[]=utils,jquery-core,jquery-migrate,moxiejs,plupload&ver=4.9.8:3)

My code:我的代码:

        <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>

Any idea?任何的想法?

div# div.channel is an invalid css selector. div# div.channel是无效的 css 选择器。 There is a space after the # which would make an invalid ID selector. #后面有一个空格,这会使 ID 选择器无效。 So one of your string concatenations is either concatenating a string with a leading space, or you are concatenating a variable that is empty.因此,您的字符串连接之一是连接带有前导空格的字符串,或者您连接的是空变量。

For instance with:例如:

jQuery('div#' + id + ' div.channel')

if id is an empty string you will get the string the error mentions.如果id是空字符串,您将获得错误提到的字符串。 Same with:同:

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

If channel_grid[i] is empty it will make an invalid css selector.如果channel_grid[i]为空,它会产生一个无效的 css 选择器。 Go back through your code and make sure your code is setup correctly to generate those variables and aren't generating empty strings.回顾您的代码并确保您的代码设置正确以生成这些变量并且没有生成空字符串。 Or use error checking before attempting to use them.或者在尝试使用它们之前使用错误检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:语法错误,jQuery中无法识别的表达式 - Uncaught Error: Syntax error, unrecognized expression in jQuery 未捕获的错误、语法错误、无法识别的表达式 [JQUERY] - Uncaught Error, Syntax Error, unrecognized expression [JQUERY] jQuery:未捕获的错误:语法错误,无法识别的表达式 - jQuery: Uncaught Error: Syntax error, unrecognized expression jQuery Uncaught语法错误,无法识别的表达式:+ - jQuery Uncaught Syntax error, unrecognized expression: + 错误未捕获的错误:语法错误,表达式无法识别 - Error Uncaught Error: Syntax error, unrecognized expression jQuery Uncaught Error语法错误,无法识别的表达式:li / label - Jquery Uncaught Error Syntax error, unrecognized expression: li/label 未捕获的错误:语法错误,jQuery升级后无法识别表达式 - Uncaught Error: Syntax error, unrecognized expression after jQuery upgrade JQUERY 未捕获的错误:语法错误,无法识别的表达式:# on javascript: void(0) - JQUERY Uncaught Error: Syntax error, unrecognized expression: # on javascript: void(0) Bootstrap 下拉菜单 Jquery Uncaught Error: Syntax error, unrecognized expression - Bootstrap dropdown Jquery Uncaught Error: Syntax error, unrecognized expression jQuery 获取:未捕获的错误:语法错误,无法识别的表达式:#/app/home - jQuery Getting: Uncaught Error: Syntax error, unrecognized expression: #/app/home
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM