How to select html nodes by ID with jquery when the id contains a dot?

后端 未结 8 1156
生来不讨喜
生来不讨喜 2020-11-22 08:22

If my html looked like this:


    

        
8条回答
  •  滥情空心
    2020-11-22 08:40

    @Tomalak in comments:

    since ID selectors must be preceded by a hash #, there should be no ambiguity here

    “#id.class” is a valid selector that requires both an id and a separate class to match; it's valid and not always totally redundant.

    The correct way to select a literal ‘.’ in CSS is to escape it: “#id\.moreid”. This used to cause trouble in some older browsers (in particular IE5.x), but all modern desktop browsers support it.

    The same method does seem to work in jQuery 1.3.2, though I haven't tested it thoroughly; quickExpr doesn't pick it up, but the more involved selector parser seems to get it right:

    $('#SearchBag\\.CompanyName');
    

提交回复
热议问题