jQuery - 挑选一个id的特定数字

时间:2012-01-05 13:09:33

标签: jquery

我在div-tag中有ID,看起来像这样:

    id="abc-0-0"
    id="abc-0-1"
    id="abc-0-2"

等等。

我想得到最后的数字(这里:0,1,2),以便将它们保存在例如变量中。

有人可以帮忙吗?

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以使用splitsplice来获取这两个数字。例如

"abc-0-1".split('-').splice(1)
// gives ["0", "1"]

答案 1 :(得分:0)

id='abc-0-0-0-0-0-9';
lastNum=id.substring(id.length-1, id.length);
/* some_string.substring(START, END);
returns a string from START to END position in some_string
where START and END are integers starting from 0 */
alert(lastNum);

答案 2 :(得分:0)

这里:

http://jsbin.com/abibek/edit#javascript,html

$("div").filter(

function ()
{
    return /^abc\-[0-9]+\-[0-9]+$/.test($(this).attr("id"))
}).map(function ()
{
    return $(this).attr('id').split('-')[2]
}).get()