Jquery Syntax error, unrecognized expression:

后端 未结 3 2063
清酒与你
清酒与你 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:52

    If I understood your question, you have an ID that contains special characters. You basically need to escape them so they're treated as literal characters rather than query selectors:

    To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \. For example, an element with id="foo.bar", can use the selector $("#foo\.bar").

    ... and in this case you also need an additional escaping level for the string delimiter, ". It's crazy but this works:

    
    
    
    
    
    
    
    
    
    Foo

    Edit: Of course, dystroy's answer —omit jQuery selection engine and use getElementByID()— is way more practical. jQuery manual links an interesting blog entry that covers this and other related techniques:

    document.getElementById() and similar functions like document.getElementsByClassName() can just use the unescaped attribute value, the way it’s used in the HTML. Of course, you would have to escape any quotes so that you still end up with a valid JavaScript string.

提交回复
热议问题