【问题标题】:jQuery Get Class That Starts With A StringjQuery获取以字符串开头的类
【发布时间】:2023-03-26 08:13:02
【问题描述】:

选择器类可以是:

c-1
c-2
c-3
(etc...)

选择器还可以有其他类,所以这里是一个可能的 HTML 示例:

<div class="foo c-2 foofoo"></div>

我想得到c 后面的号码。在这种情况下 - 我会得到2

我该怎么做?

【问题讨论】:

  • 你能接受@Ariel 对你有用的答案吗?

标签: jquery class startswith


【解决方案1】:

试试

var el = $('.c-2')//get the element whose class value has to be extracted
var val = el.attr('class').match(/\bc-(\d+)\b/)[1]

【讨论】:

  • 这真是太棒了!
【解决方案2】:

如果你确定你的班级中没有其他整数

试试

var s ="foo c-2 foofoo";
var result = /\d+(?:\.\d+)?/.exec(s);
alert(result);  //2 is the result

Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-09
    • 2013-08-12
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多