简体   繁体   English

javascript从路径数组中获取文件扩展名

[英]javascript get filename extension from array of paths

I have a page like below, I want to get all filename extensions into an array, filter them and if is a ppt (or whatever) to append the link for downloading the necessary software after the document link 我有一个像下面这样的页面,我希望将所有文件扩展名放到一个数组中,过滤它们,如果是ppt(或其他),在文档链接后附加链接以下载必要的软件

sample page 样本页面

 <p><a href="/someurl/file.ppt">Presentation</a>
 <br/>
 <a href="/someurl/file.pdf">Rules</a>
 </p>

desired result 期望的结果

<p><a href="/someurl/file.ppt">Presentation</a>
<br/>
<a href="#">Get the Microsoft® Powerpoint viewer</a>
<br/>
<a href="/someurl/file.pdf">Rules</a>
<br/>
<a href="#">Get Adobe reader</a>
</p>

so far I got here and if I try to apply any filter like 'lastIndexOf' to get the extensions I got error. 到目前为止,我到了这里,如果我尝试应用任何过滤器,如'lastIndexOf'来获得扩展我得到错误。

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++)  {
//if(links[i].lastIndexOf('.'))
console.log(links[i].href);
}

Please help, Thank you 请帮忙,谢谢

I realized that I have some <a> tags inside <li> tag like 我意识到我在<li>标签里面有一些<a>标签

<ul>
<li><a href="/someurl/file.ppt">Presentation</a><li>
</ul>

how can I get href from both situations? 我怎样才能从两种情况中得到href?

You need to get the last index of the href attribute, not on the object. 您需要获取href属性的最后一个索引,而不是对象。

links[i].href.lastIndexOf('.')

you did it in your console.log 你是在console.log中完成的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM