循环随机变量和数组以解决多重包含javascript的问题

循环随机变量和数组以解决多重包含javascript的问题,javascript,while-loop,adsense,Javascript,While Loop,Adsense,我正在使用网站上给出的说明,在访问者启用了广告拦截器的网站上显示慈善广告 虽然下面的代码工作得非常出色,但我正在寻找一种解决方案,在该解决方案中,我可以在同一页面上组合多个代码片段(以说明多个广告),而不必中断,也不必在不同的网站上多次更改 当前代码: <div style="max-width: 300px; max-height: 250px; overflow: hidden;"><div class="adunit" id="ad-300x250"> <s

我正在使用网站上给出的说明,在访问者启用了广告拦截器的网站上显示慈善广告

虽然下面的代码工作得非常出色,但我正在寻找一种解决方案,在该解决方案中,我可以在同一页面上组合多个代码片段(以说明多个广告),而不必中断,也不必在不同的网站上多次更改

当前代码:

<div style="max-width: 300px; max-height: 250px; overflow: hidden;"><div class="adunit" id="ad-300x250">

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" ></script>
<!-- Adblock4Charity - 300x250 -->
<ins class="adsbygoogle" style="display: inline-block; width: 300px; height: 250px;" data-ad-client="ca-pub-9259642480484018" data-ad-slot="2331359741"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

</div><div style="height: auto; width: auto;"><a href="http://www.savethechildren.org"><img src="http://www.adblock4charity.com/wp-content/uploads/2014/11/Save-the-Children.jpg" alt="Save the Children" width="298" height="248" /></a>
<script>
window.onload = function() {
setTimeout(function() {
var ab4c = document.querySelector("div#ad-300x250 > ins.adsbygoogle");
if (ab4c && ab4c.innerHTML.replace(/s/g, "").length == 0) {
 ab4c.style.cssText = 'display:block !important';
 ab4c.innerHTML = '<a href="http://www.savethechildren.org"><img src="http://www.adblock4charity.com/wp-content/uploads/2014/11/Save-the-Children.jpg" width="298" height="248" /></a>'; }; }, 1000);
};
</script>

(adsbygoogle=window.adsbygoogle | |[]).push({});
window.onload=函数(){
setTimeout(函数(){
var ab4c=document.querySelector(“div#ad-300x250>ins.adsbygoogle”);
if(ab4c&&ab4c.innerHTML.replace(/s/g,“”).length==0){
ab4c.style.cssText='display:block!important';
ab4c.innerHTML='';};},1000);
};
我正在寻找一个解决方案,也许还有一段工作代码,可以让我轻松地在多个站点上复制它。我正在考虑创建随机变量名,将其添加到数组中,然后在数组中循环。也许这不是最好的解决方案,但我缺乏javascript知识。

Demo on 你可能会从阅读中受益

正如您所看到的,这里的
for
循环是一个很好的选择。在本例中,我选择了所有红色方块。然后可以在循环中替换内部HTML

我希望这足以帮助您解决问题。:)

HTML

<div class="square" id="red">
    <p>Red</p>
</div>
<div class="square" id="red">
    <p>Red</p>
</div>
<div class="square" id="yellow">
    <p>Yellow</p>
</div>
JS

.square {
    width: 100px;
    height: 100px;
}
#red {
    background: red;
}
#yellow {
    background: yellow;
}
var squares = document.querySelectorAll("div.square"),
    redSquares = document.querySelectorAll("div.square#red"),
    youLikeCharities = true;

if (youLikeCharities) {
    for (var i = 0; i < redSquares.length; i++) {
        redSquares[i].innerHTML = '<img src="http://www.adblock4charity.com/wp-content/uploads/2014/11/Save-the-Children.jpg" width="100" height="100" />';
    }
};
var squares=document.querySelectorAll(“div.squares”),
redSquares=document.queryselectoral(“div.squares#red”),
YoulikeChirities=true;
如果(YoulikeChirities){
对于(变量i=0;i
谢谢布兰登,这帮了大忙!