如何切片id和抓一个数字? [英] How to slice a id and grab a number?

查看:277
本文介绍了如何切片id和抓一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery id像

I have a jQuery id like

<div id="myid-page-top-0">
    some stuff
</div>

我想使用jQuery来抓取0 #id 结尾。我尝试过像

I want to use jQuery to grab the "0" from the end of the #id. I have tried like

$('div')。slice(-1,0);

但这不行?任何想法?

推荐答案

而不是slice,这将只有工作,如果你的id包含数字0-9,

Instead of slice, which would only work if your id contains numbers 0-9, not if it is say 10 or 11, you should base it off of the dashes.

如下:

var id = "myid-page-top-0";
alert(id.split("-")[3]);

或在您的代码中:

var number = $('div').attr('id').split("-")[3];

这篇关于如何切片id和抓一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆