jQuery-选择id的特定数字
我在div标签中有ID,看起来像:jQuery-选择id的特定数字,jquery,Jquery,我在div标签中有ID,看起来像: id="abc-0-0" id="abc-0-1" id="abc-0-2" 等等 我想获取最后的数字(这里是:0、1、2),以便将它们保存在变量中 有人能帮忙吗 提前感谢。您可以使用split和splice获得这两个数字。例如 "abc-0-1".split('-').splice(1) // gives ["0", "1"] 在这里: 相同id不是好的做法D:)@user1126272查看我的完美解决方案!糟糕的解决方案这不
id="abc-0-0"
id="abc-0-1"
id="abc-0-2"
等等
我想获取最后的数字(这里是:0、1、2),以便将它们保存在变量中
有人能帮忙吗
提前感谢。您可以使用
split
和splice
获得这两个数字。例如
"abc-0-1".split('-').splice(1)
// gives ["0", "1"]
在这里:
相同id不是好的做法D:)@user1126272查看我的完美解决方案!糟糕的解决方案这不会返回符合条件的完整元素数组,但只返回一个!OP需要一个完整匹配元素的数组!谢谢你们!我要这个。这对我的意图是最好的。
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);
$("div").filter(
function ()
{
return /^abc\-[0-9]+\-[0-9]+$/.test($(this).attr("id"))
}).map(function ()
{
return $(this).attr('id').split('-')[2]
}).get()