Get part of a string using jQuery

后端 未结 3 1725
孤独总比滥情好
孤独总比滥情好 2021-02-03 12:20

HTML code:

How can I get number \"45\" of string using jQuery?

3条回答
  •  不知归路
    2021-02-03 12:37

    You don't need (or particularly want) jQuery for this (it's very useful for lots of other things, just not particularly for this). Straight JavaScript and DOM:

    var div = document.getElementById('block-id-45');
    var n = div.id.lastIndexOf('-');
    var target = div.id.substring(n + 1);
    

    Live example: http://jsbin.com/osozu

    If you're already using jQuery, you can replace the first line with:

    var div = $('#block-id-45')[0];
    

    ...but there's little if any reason to.

提交回复
热议问题