What is the best way to cut the file name from 'href' attribute of an 'a' element using jQuery?

后端 未结 6 536
傲寒
傲寒 2021-01-16 19:32

For example I\'ve got the simple code:

6条回答
  •  无人及你
    2021-01-16 20:22

    var files = $('a[href]').map(function() {
        return this.pathname.match(/[^\/]*$/)[0];
    });
    

    a[href] will exclude anchors without hrefs (such as named anchors), and pathname will remove query strings and fragments (?query=string and #fragment).

    After this, files will contain an array of filenames: ['file01.pdf', 'file02.pdf'].

提交回复
热议问题