将类别名称分配给元素时转义方括号 [英] Escape square brackets when assigning a class name to an element
本文介绍了将类别名称分配给元素时转义方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人用jQuery设置类名时知道如何转义方括号字符吗?
Does anyone know how to escape the square bracket character when setting a class name with jQuery?
请尝试以下操作:
$('#txtFirstname').addClass('test[someval]')
然后
$('#txtFirstname').attr('class')
您会看到那里的课程.
现在尝试
$('#txtFirstname').hasClass('test[someval]')
失败
我能想到的唯一原因是方括号.
The only reason I can think of is the square brackets.
您需要这些用于我的jQuery验证.
And I need these for my jQuery validation you see.
推荐答案
使用两个(2)反斜杠转义.
Escape with TWO (2) backslashes.