【发布时间】:2020-04-18 19:26:57
【问题描述】:
我一定是想多了。我需要选择类名称为cbs-Line1Link 的所有锚标记,如果它与文件类型(ppt、pptx、doc、docx 等)匹配,则将?Web=1 添加到 HREF 的末尾。所以,ppt 变成了pptx?Web=1
<a class="cbs-Line1Link" href="index.pptx">pptx title</a>
--------------------------------------------------------
const items = document.querySelectorAll(".cbs-Line1Link");
items.forEach(item => {
let href = item.getAttribute("href")
if (href.substring(href.length - 5) == '.pptx') {
console.log(item)
return item = item.getAttribute("href").replace(/\bpptx\b/, 'pptx?Web=1')
}
})
提前谢谢你!
【问题讨论】:
标签: javascript replace foreach substring