jquery get number from id

前端 未结 6 1995
长情又很酷
长情又很酷 2020-12-03 07:56

how can i get the number from a div tag\'s id?

example:

how can i get the 1 and store it in

6条回答
  •  难免孤独
    2020-12-03 08:03

    // replace all non-digits with nothing
    alert($('div').attr("id").replace(/\D/g,''));
    

    If you like, you can just grab the numbers at the end:

    // 
    alert($('div').attr("id").match(/\d+$/)); // alerts "555"

提交回复
热议问题