-
使用以下命令选择DOM上的所有节点:
document.getElementsByTagName('*')
-
在它们之间循环。如果有src,则在src前面更新repo名称。如果是href,则使用href执行相同的操作。
const repoName = '/Demo_App';
window.addEventListener('load', () => {
const all = document.getElementsByTagName('*');
for (const node of all) {
const src = node.getAttribute('src');
const href = node.getAttribute('href');
if (src) node.setAttribute('src', `${repoName}${src}`);
if (href) node.setAttribute('href', `${repoName}${href}`);
}
});
<body>
<a href="https://google.com"></a>
<img src="https://google.com" />
<a href="https://google.com"></a>
<img src="https://google.com" />
<a href="https://google.com"></a>
<img src="https://google.com" />
<a href="https://google.com"></a>
<img src="https://google.com" />
</body>