如何在 html 代码中替换目标域的所有 URL?

How do I replace all URLs of a target domain in html code?

我正在尝试用他的标题中描述的 URL 替换所有带有 domain1.com 的 HREF(如果它有标题)。

var els = document.getElementsByTagName('a'),
  for (els in url) {
    if (url.includes(domain1)); {
      var titulo = url.attr('title');
      var enlace = url.attr('href');
      url.replace(enlace, titulo);
    }
  }
<p style="margin:40px;" line-height:2px;=""><img src="https://www.domain1.com/738956_1.jpg" data-width="1070" data-height="1387" data-image="738956-sanKH" alt="738956-sanKH.jpg">XS <a href="https://www.domian1.com/plants" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test1">texto</a><br>
  <img src="https://www.domain1.com/738956_1.jpg" data-width="1077" data-height="1693" data-image="738956-iMkWh" alt="738956-iMkWh.jpg">S <a href="https://www.domain1.com/flowera" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test2">texto</a></p>

我是 javascript 的初学者。肯定有很多格式错误。提前致谢

如果您的 DOM 在文档加载后拥有所有链接,那么您可以这样做:

window.onload = bindEvents;

function bindEvents() {
  // replace href with title
  bindAnchorTitleAsHref();
}

function bindAnchorTitleAsHref() {
  const els = document.getElementsByTagName('a');
  for (let i = 0; i < els.length; i += 1) {
      const title = els[i].getAttribute('title');
      const href = els[i].getAttribute('href')
      
      els[i].setAttribute('href', title);
      
      console.log('New Href set :', els[i].getAttribute('href'), 'instead of', href);
  }
}
<a 
  href="https://www.domian1.com/plants"
  title="https://www.newdomain.com/test1"
  target="_blank" 
  rel="nofollow noopener"
>
  texto
</a>

如果您只想更新 link 的域,那么您可以使用 the URL class:

document.addEventListener('DOMContentLoaded', () => {
  document.querySelectorAll('a[title]').forEach(el => {
    const href = new URL(el.href);
    if ((/[\^\.]domain1.com$/i).test(href.host)) {
      const title = new URL(el.title);
      href.host = title.host;
      el.href = href.toString();
    }
  });
});
<p style="margin:40px;" line-height:2px;=""><img src="https://www.domain1.com/738956_1.jpg" data-width="1070" data-height="1387" data-image="738956-sanKH" alt="738956-sanKH.jpg">XS <a href="https://www.domian1.com/plants" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test1">texto</a><br>
  <img src="https://www.domain1.com/738956_1.jpg" data-width="1077" data-height="1693" data-image="738956-iMkWh" alt="738956-iMkWh.jpg">S <a href="https://www.domain1.com/flowera" target="_blank" class="link" rel="nofollow noopener" title="https://www.newdomain.com/test2">texto</a></p>

这里是经过测试的 JavaScript 代码,如果你想支持 Internet Explorer 浏览器,请将 for..of 替换为 for..in

  var domain1 = 'domain1.com';
  var els = document.getElementsByTagName('a');
    
  for (var url of els) { 
    var title = url.getAttribute('title');
  
    if (title && url.getAttribute('href').search(domain1) !== -1) {
       url.setAttribute('href', title);
    }
  }