【问题标题】:How to shield "." char? [duplicate]如何屏蔽“。”字符? [复制]
【发布时间】:2011-11-28 07:38:11
【问题描述】:

可能重复:
How do I get jQuery to select elements with a . (period) in their ID?

我尝试运行以下代码:

$('#info-mail.ru .domain-info').toggle();

example here

据我所知,#info-mail.ru 被解释为 id="info-mail" 和 class="ru",但我的结构如下:

<div id="info-mail.ru">
    <p class="domain-info">
        Some cool info
        Some cool info
        Some cool info
        Some cool info
    </p>
</div>

如何屏蔽“.”选择器语句中的字符?或者唯一的方法是替换所有“。”用“_”(例如)?

TIA!

【问题讨论】:

标签: javascript jquery


【解决方案1】:

用双反斜杠转义.,一个用于文字,另一个用于jQuery:

$('#info-mail\\.ru .domain-info').toggle();

详情请见jQuery FAQ

【讨论】:

  • 删除最后一个 \\ : $('#info-mail\\.ru .domain-info').toggle();最后一个是类选择器。
  • 谢谢!工作完美!你能解释一下 - 这个“屏蔽”语法是在哪里描述的(即“\\”)?
  • @dizpers - 它们不被称为屏蔽字符,而是转义字符
  • @dizpers - 见jQuery FAQ。它归结为用文字表示 \ jQuery 将其解释为转义下一个字符的命令,在本例中为 .
【解决方案2】:

我认为您需要在 . 之前使用两个反斜杠。

所以选择器是$('#info-mail\\.ru .domain-info').toggle();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-13
    • 2017-12-15
    • 2023-03-21
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    相关资源
    最近更新 更多