【问题标题】: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。
我该怎么做?
【问题讨论】:
标签:
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