如何从jquery中的类名获取数字?

时间:2015-05-08 14:17:27

标签: jquery regex match

我尝试了几种解决方案,但代码仍然不起作用......

这是我到目前为止所得到的:

$('.next-arrow').click(function (){
    var num = $(this).closest('[class^=step-]').match(/\d+/)[0];
    console.log(num);
});

如果我在没有.match()的情况下运行它,它可以正常运行并告诉我我在控制台日志中处于“step-1”,但添加.match()不会返回任何内容。

我在这里要做的是检查我在“步骤1,步骤2,......”的位置,并指定该数字以隐藏当前步骤并显示下一步。

1 个答案:

答案 0 :(得分:1)

你忘了带一个找到的元素而不是元素本身:

$('.next-arrow').click(function (){
    var num = $(this).closest('[class^=step-]').attr("class").match(/\d+/)[0];
    console.log(num);
});