jQuery (Javascript) 从路径名解析文件名
jQuery (Javascript) parse document name from pathname
对于 url 喜欢 example.com/this/is/my/path.html
var pageName = $(location).attr('pathname')
将以下结果赋给变量。
this/is/my/path.html
我的问题:如何进一步解析 pageName
以仅生成 path.html
?
或者,是否有另一个 .attr()
jQuery 参考资料只能为您提供该信息?
尝试:
var theUrl = $(this).attr('href');
var index = theUrl.lastIndexOf("/") + 1;
var filename = theUrl.substr(index);
console.log( 'link file name is '+filename );
根据以下回复,我发现:
var pageName = $(location).attr('pathname').split('/').pop();
...得到了我想要的结果。
对于 url 喜欢 example.com/this/is/my/path.html
var pageName = $(location).attr('pathname')
将以下结果赋给变量。
this/is/my/path.html
我的问题:如何进一步解析 pageName
以仅生成 path.html
?
或者,是否有另一个 .attr()
jQuery 参考资料只能为您提供该信息?
尝试:
var theUrl = $(this).attr('href');
var index = theUrl.lastIndexOf("/") + 1;
var filename = theUrl.substr(index);
console.log( 'link file name is '+filename );
根据以下回复,我发现:
var pageName = $(location).attr('pathname').split('/').pop();
...得到了我想要的结果。