JQUERY: Uncaught Error: Syntax error, unrecognized expression

后端 未结 7 1070
予麋鹿
予麋鹿 2020-12-15 17:28
console.log($(\'\"#\'+d+\'\"\'));

in html i have

5

eeeeeeeeeee

相关标签:
7条回答
  • 2020-12-15 17:34

    Try this (ES5)

    console.log($("#" +  d));
    

    ES6

    console.log($(`#${d}`));
    
    0 讨论(0)
  • 2020-12-15 17:36

    The "double quote" + 'single quote' combo is not needed

    console.log( $('#'+d) ); // single quotes only
    console.log( $("#"+d) ); // double quotes only
    

    Your selector results like this, which is overkill with the quotes:

    $('"#abc"') // -> it'll try to find  <div id='"#abc"'>
    
    // In css, this would be the equivalent:
    "#abc"{ /* Wrong */ } // instead of:
    #abc{ /* Right */ }
    
    0 讨论(0)
  • 2020-12-15 17:41

    This can also happen in safari if you try a selector with a missing ], for example

    $('select[name="something"')
    

    but interestingly, this same jquery selector with a missing bracket will work in chrome.

    0 讨论(0)
  • 2020-12-15 17:44

    Try using:

    console.log($("#"+d));
    

    This will remove the extra quotes you were using.

    0 讨论(0)
  • 2020-12-15 17:44

    I had to look a little more to solve my problem but what solved it was finding where the error was. Here It shows how to do that in Jquery's error dump.

    In my case id was empty and $("#" + id);; produces the error.

    It was where I wasn't looking so that helped pinpoint where it was so I could troubleshoot and fix it.

    0 讨论(0)
  • 2020-12-15 17:45

    If you're using jQuery 2.1.4 or above, try this:

    $("#" + this.d);
    

    Or, you can define var before using it. It makes your code simpler.

    var d = this.d
    $("#" + d);
    
    0 讨论(0)
提交回复
热议问题