私信  •  关注

mstephen19

mstephen19 最近创建的主题
mstephen19 最近回复了
4 年前
回复了 mstephen19 创建的主题 » 使用Jquery附加到所有href和src属性
  1. 使用以下命令选择DOM上的所有节点:
document.getElementsByTagName('*')
  1. 在它们之间循环。如果有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>