Using Javascript to get and replace strings
P粉364129744
P粉364129744 2023-09-07 11:19:05
0
1
558

My website has a lot of links and I'm trying to replace all the a href targets _top with _blank. How can I implement this functionality using JavaScript?

It will find all the _top in my page and replace them with _blank

I tried the following:

// const string = document.getElementsByTagName('a')[0].innerHTML
const string = document.querySelector("_top"); //获取字符串
var newstring = string.replace(/_top/, '_blank'); //尝试在整个页面中替换字符串
const myTimeout = setTimeout(selecteAd, 2000); //设置延时以确保网页完全加载
<!-- 示例字符串 -->
<a data-asoch-targets="imageClk" href="https://www.eample.com" target="_top" x-ns-8l6in-e="2"><canvas class="ns-8l6in-e-3 image" x-ns-8l6in-e="3" width="600" height="314"></canvas></a>

renew:

Data comes from Google AdSense's async code. (Found similar solutions in other parts of Stackoverflow, but I still can't replace. How to get DIV child elements using JavaScript)

P粉364129744
P粉364129744

reply all(1)
P粉517814372

Use querySelectorAll

$(function() {
  document.querySelectorAll("a[target=_top]").forEach(function(elem) {
    this.target="_blank";
  });
  });
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template