jQuery 中 ID 的正则表达式
<span id="aaa_1_bbb">aaa</span>
<span id="aaa_2_bbb">aaa</span>
<span id="aaa_3_bbb">aaa</span>
<span id="aaa_4_bbb">aaa</span>
<span id="aaa_5_bbb">aaa</span>
<span id="aaa_6_bbb">aaa</span>
<span id="bbb_1_bbb">aaa</span>
<span id="ccc_1_bbb">aaa</span>
<span id="ddd_1_bbb">aaa</span>
<span id="aaa_1_xxx">aaa</span>
$('span[id="aaa_*_bbb"]').css('background-color', 'red');
我必须如何修改这个 jQuery?我想仅为 aaa_*_bbb ID 添加 css (背景颜色:红色)。 * = 数字
<span id="aaa_1_bbb">aaa</span>
<span id="aaa_2_bbb">aaa</span>
<span id="aaa_3_bbb">aaa</span>
<span id="aaa_4_bbb">aaa</span>
<span id="aaa_5_bbb">aaa</span>
<span id="aaa_6_bbb">aaa</span>
<span id="bbb_1_bbb">aaa</span>
<span id="ccc_1_bbb">aaa</span>
<span id="ddd_1_bbb">aaa</span>
<span id="aaa_1_xxx">aaa</span>
$('span[id="aaa_*_bbb"]').css('background-color', 'red');
how must i modify this jQuery? i would like that add css (background-color:red) only for aaa_*_bbb ID. * = numbers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
按照 HTML 的形式,您可以使用 属性开头 和属性以选择器结尾:
但是,如果HTML 由您掌控应考虑向这些元素添加
class
,或向其父元素添加id
或class
,以便您可以以更直接的方式选择它们。查看实际操作。
As the HTML stands, you can use a combination of the attribute starts with and attribute ends with selectors:
However, if the HTML is under your control you should consider adding a
class
to these elements, or anid
orclass
to their parent, so that you can select them in a more straightforward manner.See it in action.
您可以使用
.filter
You could use
.filter
你可以这样做:
它获取每个 span 元素并检查前 3 个字符是否为“aaa”,后 3 个字符是否为“bbb”,如果是这种情况则分配 CSS。
You could do something like:
It takes every span element and checks whether or not the first 3 characters are "aaa" and the last 3 are "bbb" and assigns the CSS if this is the case.
我认为你需要使用这个: http://james.padolsey.com/ javascript/regex-selector-for-jquery/
I think you need to use this: http://james.padolsey.com/javascript/regex-selector-for-jquery/
更好的选择是在这些元素上设置特定的类,例如
然后,您可以执行
$('span.aaabbb').css('background-color', 'red');
The better option would be to set a specific class on those elements, like
Then, you can just do
$('span.aaabbb').css('background-color', 'red');