jQuery 选择器来定位以前缀开头的任何类名(多个存在)?
我正在考虑一个选择语句,该语句将基于字符串前缀在单个类属性值中定位多个 css 类名之一。
例如,我希望从以下示例链接中获取任何带有 detail-
前缀的类名。
<a href="eg.html" class="detail-1 pinkify another">
<a href="eg.html" class="something detail-55 minded">
<a href="eg.html" class="swing narrow detail-Z">
<a href="eg.html" class="swing narrow detail-Z detail-88 detail-A">
这让人想起 [class|="detail"]
< /a> 前缀选择器适用于标量属性值,也适用于 .hasClass(className)
,但我的问题需要同时应用这两个概念。
注意: detail-
前缀不一定是该组的第一个类名。
I'm considering one selection statement that would target one of many css class names in a single class attribute value based on a string prefix.
For example, I want any detail-
prefixed class names to get targeted from the following sample links.
<a href="eg.html" class="detail-1 pinkify another">
<a href="eg.html" class="something detail-55 minded">
<a href="eg.html" class="swing narrow detail-Z">
<a href="eg.html" class="swing narrow detail-Z detail-88 detail-A">
It's reminiscent of how [class|="detail"]
prefix selector works on a scalar attribute value, and also of .hasClass(className)
, but my question needs both concepts applied simultaneously.
Note: The detail-
prefix won't necessarily be the first class name of the bunch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
class
属性的设计方式,您需要使用至少两个其他属性选择器(请注意[class*="detail-"]):
选择具有以
detail-
开头的class
属性的元素
前缀的类>详细信息-
。类名由空格分隔根据 HTML 规范,因此有重要的空格字符。如果您想将其转换为自定义选择器表达式,您可以执行以下操作:
然后像这样选择它:
或者如果您想将其放入插件中:
然后像这样调用它:
Because of the way the
class
attribute is designed, you'll need to make use of at least two other attribute selectors (notice the whitespace in[class*=" detail-"]
):This selects
<a>
elements with aclass
attribute thatdetail-
, ordetail-
. Class names are separated by whitespace per the HTML spec, hence the significant space character.If you'd like to turn this into a custom selector expression, you can do this:
Then select it like this:
Or if you'd like to place this in a plugin:
Then call it like this:
您可以添加自己的选择器,在您的情况下,您可以为属性添加正则表达式选择器:
然后你可以这样做:
http://jsfiddle.net/ambigously/ ArtRS/1/
我在 SO 上的某个地方看到了那篇文章,但我不记得在哪里了。
You can add your own selectors, in your case you could add a regular expression selector for attributes:
Then you could do something like this:
http://jsfiddle.net/ambiguous/ArtRS/1/
I came across that article somewhere on SO but I can't remember where.
您还可以编写一个小插件,使用
.map()
来遍历这些类。进而:
You could also write a small plugin that uses
.map()
to walk through the classes.And then:
尝试通配符选择器
http://www.emadibrahim.com/2009/07 /25/jquery-通配符选择器/
try wildcard selector
http://www.emadibrahim.com/2009/07/25/jquery-wildcard-selector/