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

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

For example I\'ve got the simple code:

6条回答
  •  被撕碎了的回忆
    2021-01-16 20:22

    Though you don't have them in this case, in general you would want to strip off the ?search and #hash parts before looking for the last slash to get the file's leafname.

    This is very easy using the built-in properties of the link object like pathname instead of processing the complete href:

    var a= document.getElementById('link1');    // or however you decide to reference it
    var filename= a.pathname.split('/').pop();  // get last segment of path
    

提交回复
热议问题