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

My website has a lot of links and I'm trying to replace all thea hreftargets_topwith_blank. How can I implement this functionality using JavaScript?

It will find all the_topin 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); //设置延时以确保网页完全加载
 

renew:

Data comes from Google AdSense'sasynccode. (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
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!