首页 > 解决方案 > 更改推荐链接

问题描述

我有问题。html或javascript有什么办法吗?

我有adf.ly推荐链接(示例)adf.ly/12345 + link:。

要下载我的源下载文件1,我输入链接为:adf.ly/12345/https://example.com/downloadfile1

对于file2链接我输入adf.ly/12345/https://example.com/downloadfile2

..等等许多链接。但是有一天我需要将我的推荐链接从 adf.ly/12345 更改为 adf.ly/6789 所以我需要将上面的链接一一更改:

adf.ly/6789/https://example.com/downloadfile1

adf.ly/6789/https://example.com/downloadfile2

.. 等等。

我的问题是,有没有办法使用 html 或 JavaScript自动更改任何从adf.ly/12345+urlto开始的链接?adf.ly/6789+link

我尝试搜索该主题,但我只是初学者。我找不到任何或可能是错误的搜索。

标签: javascripthtml

解决方案


假设您使用两个输入标签,首先获取初始 url 的输入,然后在下一个输入字段中插入新的 url,当单击按钮时,它将在任何地方用新的 url 替换现有的 url。

function replaceUrl () {
    
    let initial = document.getElementById("initial").value;
    let replace = document.getElementById("replace").value;
    let url = ['shorte.st/abc123/https://example.com/downloadfile2', 'adf.ly/12345/https://example.com/downloadfile2']
    url = url.map((u) => u.includes(initial) ? u.replace(initial, replace) : u)
    console.log(url)
}
Enter first part <input type="text" id="initial"/>
Enter second part <input type="text" id="replace"/>
<input type="button" value="Change" onclick="replaceUrl()">


推荐阅读