Jquery Syntax error, unrecognized expression:

后端 未结 3 2062
清酒与你
清酒与你 2020-12-06 00:12

Why do I get this error and how can I test for it so it wont break, I tried checking for null but obviously that wont work, thanks.

Please don\'t advice to not write

3条回答
  •  攒了一身酷
    2020-12-06 00:37

    W3C has defined a new function for this this: CSS.escape(). In your example code, it would look like this:

    var jsonTest = [
      {
        "myId": "''''''\"\"\"\"'''''''''''''\"#####$'''''",
      }
    ];
    
    $('#' + CSS.escape(jsonTest[0].myId)).length; // Works 
    

    Browsers don't yet support it, but there is a polyfill library that you can use in the mean time: https://github.com/mathiasbynens/CSS.escape

    I've used it with success in my project.

提交回复
热议问题