为什么带有句点的这个jQuery选择器不起作用 [英] Why doesn't this jquery selector with a period work

查看:89
本文介绍了为什么带有句点的这个jQuery选择器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ID为ID的div

I have a div with an ID

<div id="updates-pane-user.followed_on">

此jquery选择器有效

this jquery selector works

$("[id^='updates-pane']")

但这不是

$("#updates-pane-user.followed_on")

我看不出有什么问题.ID名称可以包含句点,对吗?我缺少明显的东西吗?

I don't see what is wrong.. id names can include periods right? Am I missing something obvious?

推荐答案

在后一个选择器中,.用于表示一个类.因此,它正在寻找.followed_on类,显然找不到该类,因此没有任何匹配项.

In the latter selector, . is used to denote a class. So it's looking for the .followed_on class, which it obviously doesn't find and so nothing is matched.

为解决此问题,我认为您应使用双重反斜杠来使圆点转义:

In order to fix this, I think you should escape the dot with a double-backslash:

$("#updates-pane-user\\.followed_on")

根据 jQuery文档:

使用任何元字符(例如!#$%&'()* +,./:;< =>?@ [] ^`{|}〜)作为文字部分名称中,必须使用两个反斜杠将其转义:\\例如,具有id ="foo.bar"的元素可以使用选择器$(#foo \\.bar").

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

通常,请尝试在ID中使用句点或其他特殊字符,以免造成混乱. 允许"与良好做法"不同.

In general, try not to use periods or other special characters in your IDs to avoid confusion all around. "Allowed" is not the same as "good practice."

这篇关于为什么带有句点的这个jQuery选择器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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