简体   繁体   English

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

[英]Looping random variable and array to conquer issue with multiple inclusion of javascript

I'm using the instructions given on a website to show charity adverts on sites where visitors have ad-blockers enabled. 我正在使用网站上提供的说明在访问者启用了广告拦截器的网站上显示慈善广告。

Whilst the following code works brilliantly, I'm looking for a solution where I can combine multiple code snippets on the same page (to account for multiple adverts) without it breaking and without having to change it multiple times for inclusions on different websites. 尽管以下代码非常出色,但我正在寻找一种解决方案,可以在同一页面上合并多个代码段(以解决多个广告问题),而又不会破坏代码段,而不必为包含在不同网站上而对其进行多次更改。

The current code: 当前代码:

<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>

I'm looking for a solution and perhaps a working piece of code to work from that would allow me to replicate it on multiple sites with ease. 我正在寻找一种解决方案,也许正在寻找可以运行的代码片段,这将使我能够轻松地在多个站点上复制它。 I'm thinking something on the lines of creating random variable names, adding it to an array and looping through the array. 我正在考虑创建随机变量名称,将其添加到数组并遍历数组的内容。 Perhaps this isn't the best solution but I'm lacking on javascript knowledge. 也许这不是最好的解决方案,但我缺乏JavaScript知识。

Demo on JSFiddle JSFiddle上的演示

You'd probably benefit from reading about querySelectorAll . 您可能会从阅读有关querySelectorAll的内容中受益。

As you can see, a for loop is a good option here. 如您所见,在这里for循环是一个不错的选择。 In the example, I selected all red squares. 在示例中,我选择了所有红色方块。 Then the inner HTML can be replaced in the loop. 然后可以在循环中替换内部HTML。

I expect that this is enough to help you solve your problem. 我希望这足以帮助您解决问题。 :) :)

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>

CSS CSS

.square {
    width: 100px;
    height: 100px;
}
#red {
    background: red;
}
#yellow {
    background: yellow;
}

JS JS

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" />';
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM