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

时间:2021-02-10 18:11:40

I tried several solutions but the code still doesn't work...

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

Here is what I got so far:

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

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

If I run it without the .match() it works fine and tells me that I'm at "step-1" on console log, but adding .match() doesn't return anything.

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

What I'm trying to do here is to check where I'm at "step-1, step-2,..." and assign that number to hide the currently step and show the next step.

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

1 个解决方案

#1


You just forgot to take a class of the found element instead of the element itself:

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

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

#1


You just forgot to take a class of the found element instead of the element itself:

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

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