如何使用id作为选择器使用哈希时? [英] How to take id as selector when used hash?

查看:152
本文介绍了如何使用id作为选择器使用哈希时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有以下html:

<div id='test#1'>test1</div>
<div id='test#2'>test2</div>

现在,以下操作无效,为什么?

Now, the following wouldn't work, why?

css ....

#test#1{color: red;}

jQuery ....

jQuery....

$('#test#2').css('color','blue');


推荐答案

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

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

css方式:

#test\#1{
    color: red;
}

jquery方法:

$('#test\\#2').css('color','blue');

演示

建议:

Recommendation:

在您的ID 详细了解详情


ID和NAME令牌必须以字母([A-Za-z])开头,后跟任意数量的字母,数字([0-9]),连字符( - ),下划线(_),冒号(:)和句点(。)。

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").






所以,你可能还想知道id =test.1可以使用,对于这个也应该如上所述转义字符。


So, you may be also wondering id="test.1" can be used and for this too you should escape the character as described above.

这篇关于如何使用id作为选择器使用哈希时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆