How to slice a id and grab a number?
问题 I have a jQuery id like <div id="myid-page-top-0"> some stuff </div> I want to use jQuery to grab the "0" from the end of the #id . I have tried like $('div').slice(-1,0); But this doesn't work ? Any ideas ? 回答1: 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. Something like: var id = "myid-page-top-0"; alert(id.split("-")[3]); or in your code something like: var number = $('div').attr('id').split("-")[3