jQuery 中 ID 的正则表达式

发布于 2024-12-11 12:41:46 字数 716 浏览 1 评论 0原文

<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 (背景颜色:红色)。 * = 数字

实时: http://jsfiddle.net/HB3xf/2/

<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

LIVE: http://jsfiddle.net/HB3xf/2/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十级心震 2024-12-18 12:41:46

按照 HTML 的形式,您可以使用 属性开头属性以选择器结尾:

$('span[id^="aaa_"]').filter('[id$="_bbb"]').css('background-color', 'red');

但是,如果HTML 由您掌控应考虑向这些元素添加 class ,或向其父元素添加 idclass ,以便您可以以更直接的方式选择它们。

查看实际操作

As the HTML stands, you can use a combination of the attribute starts with and attribute ends with selectors:

$('span[id^="aaa_"]').filter('[id$="_bbb"]').css('background-color', 'red');

However, if the HTML is under your control you should consider adding a class to these elements, or an id or class to their parent, so that you can select them in a more straightforward manner.

See it in action.

灯角 2024-12-18 12:41:46

您可以使用 .filter

$('span').filter(function () { return /aaa_\d+_bbb/.test(this.id); }).css('background-color', 'red');

You could use .filter

$('span').filter(function () { return /aaa_\d+_bbb/.test(this.id); }).css('background-color', 'red');
っ〆星空下的拥抱 2024-12-18 12:41:46

你可以这样做:

$('span').each(function() {

    var span_id = $(this).attr('id');
    if (span_id.substring(0, 3) == "aaa" && span_id.substring(span_id.length - 3, span_id.length) == "bbb") {
        $(this).css('background-color', 'red');
    }

});

它获取每个 span 元素并检查前 3 个字符是否为“aaa”,后 3 个字符是否为“bbb”,如果是这种情况则分配 CSS。

You could do something like:

$('span').each(function() {

    var span_id = $(this).attr('id');
    if (span_id.substring(0, 3) == "aaa" && span_id.substring(span_id.length - 3, span_id.length) == "bbb") {
        $(this).css('background-color', 'red');
    }

});

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.

夜司空 2024-12-18 12:41:46

更好的选择是在这些元素上设置特定的类,例如

<span id="aaa_1_bbb" class="aaabbb">aaa</span>
<span id="aaa_2_bbb" class="aaabbb">aaa</span>
<span id="aaa_3_bbb" class="aaabbb">aaa</span>
<span id="aaa_4_bbb" class="aaabbb">aaa</span>
<span id="aaa_5_bbb" class="aaabbb">aaa</span>
<span id="aaa_6_bbb" class="aaabbb">aaa</span>
<span id="bbb_1_bbb">aaa</span>
<span id="ccc_1_bbb">aaa</span>
<span id="ddd_1_bbb">aaa</span>

然后,您可以执行 $('span.aaabbb').css('background-color', 'red');

The better option would be to set a specific class on those elements, like

<span id="aaa_1_bbb" class="aaabbb">aaa</span>
<span id="aaa_2_bbb" class="aaabbb">aaa</span>
<span id="aaa_3_bbb" class="aaabbb">aaa</span>
<span id="aaa_4_bbb" class="aaabbb">aaa</span>
<span id="aaa_5_bbb" class="aaabbb">aaa</span>
<span id="aaa_6_bbb" class="aaabbb">aaa</span>
<span id="bbb_1_bbb">aaa</span>
<span id="ccc_1_bbb">aaa</span>
<span id="ddd_1_bbb">aaa</span>

Then, you can just do $('span.aaabbb').css('background-color', 'red');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文