jQuery 为元素分配类名时转义方括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1466103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:31:44  来源:igfitidea点击:

Escape square brackets when assigning a class name to an element

jquery

提问by Duncan

Does anyone know how to escape the square bracket character when setting a class name with jQuery?

有谁知道在使用 jQuery 设置类名时如何转义方括号字符?

Try the following:

请尝试以下操作:

$('#txtFirstname').addClass('test[someval]')

then

然后

$('#txtFirstname').attr('class')

you'll see the class is there.

你会看到课程在那里。

Now try

现在试试

$('#txtFirstname').hasClass('test[someval]')

FAIL

失败

The only reason I can think of is the square brackets.

我能想到的唯一原因是方括号。

And I need these for my jQuery validation you see.

你看,我需要这些来进行 jQuery 验证。

回答by digiogo

The introduction of jQuery API pagegives the above explanation about this:

引进的jQuery API页面给出了关于这个上面的解释:

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

要使用任何元字符(例如 !"#$%&'()*+,./:;<=>?@[]^`{|}~ )作为名称的文字部分,它必须用两个反斜杠转义:\\。

回答by Eric Kigathi

If the selector is contained within a variable (or if you can get it into a variable), the code below may be helpful:

如果选择器包含在变量中(或者如果您可以将其放入变量中),下面的代码可能会有所帮助:

selector_name = $this.attr('class');
//e.g selector_name = users[0][first:name]

escaped_selector_name = selector_name.replace(/(:|\.|\[|\])/g,'\');
//e.g escaped_selector_name = users\[0\]\[first\:name\]

In short we prefix all special characters with double backslash. Here's some additional documentation from the jQuery site:

简而言之,我们用双反斜杠作为所有特殊字符的前缀。这是来自 jQuery 站点的一些附加文档:

http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

回答by álvaro González

Short answer: you don't need to escape bracketsbecause the argument is a class name, not a selector. It didn't work for you because of a bug in earlier jQuery versions that has long been fixed.

简短回答:您不需要转义括号,因为参数是类名,而不是选择器。它对您不起作用,因为早先已修复的早期 jQuery 版本中的错误。

To demonstrate it, here's your code running under different versions:

为了演示它,这是您在不同版本下运行的代码:

  • jQuery/1.3.2 (buggy):
  • jQuery/1.3.2(越野车):

$('#txtFirstname').addClass('test[someval]')
$('#txtFirstname').attr('class')
console.log($('#txtFirstname').hasClass('test[someval]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<div id="txtFirstname"></div>

  • jQuery/1.4.0 (fixed):
  • jQuery/1.4.0(固定):

$('#txtFirstname').addClass('test[someval]')
$('#txtFirstname').attr('class')
console.log($('#txtFirstname').hasClass('test[someval]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<div id="txtFirstname"></div>

So the issued was apparently fixed on jQuery/1.4.0. I found a ticketregarding this (though it's tagged as being fixed on 1.4.2 rather than 1.4.0).

所以这个问题显然是在 jQuery/1.4.0 上修复的。我找到了一张关于此的票(尽管它被标记为已修复为 1.4.2 而不是 1.4.0)。

I insist: the rule to quote or escape metacharacters with \\in order to use them as a literal part of a name only applies to selectors. However, the .hasClass()method does not receive a selector as argument but as class name:

我坚持:引用或转义元字符\\以便将它们用作名称的文字部分的规则仅适用于selectors。但是,.hasClass()方法不会接收选择器作为参数,而是作为类名:

.hasClass( className )

    className
    Type: String
    The class name to search for.
.hasClass( className )

    className
    Type: String
    The class name to search for.