How do I select an element with special characters in the ID?

前端 未结 2 1095
野趣味
野趣味 2020-12-10 22:05

I have a page with a grid containing a lot if input with \"strange names\" to simulate an array:

 

        
2条回答
  •  情深已故
    2020-12-10 22:36

    The problem is that brackets have special meaning in CSS (as attribute selectors) and dots do too, as class selectors. Try $.escapeSelector:

    $('#' + $.escapeSelector('Punteggi[@counter].Descrizione'))
    

    This will escape the selector so the special characters don't affect the selection. You could also try using an attribute selector and wrapping that in quotes:

    $('[id="Punteggi[@counter].Descrizione"]')
    

    This will literally match that ID and won't treat any of the special characters as special characters.

提交回复
热议问题