摘要:[JavaScript]幫GOOGLE廣告加上關閉ICON
有個老朋友丟了一個小問題給我
問說能不能幫GOOGLE廣告加個ICON
讓使用者可以關閉
程式碼如下
function getElementsByClass( searchClass, domNode, tagName) {
if (domNode == null) domNode = document;
if (tagName == null) tagName = '*';
var el = new Array();
var tags = domNode.getElementsByTagName(tagName);
var tcl = " "+searchClass+" ";
for(i=0,j=0; i<tags.length; i++) {
var test = " " + tags[i].className + " ";
if (test.indexOf(tcl) != -1)
el[j++] = tags[i];
}
return el;
}
/* 非同步 */
(function(){
var adElement = getElementsByClass("adsbygoogle");
var Obj;
for(var i = 0, adLen=adElement.length; i < adLen; i++){
Obj = document.createElement("img");
Obj.className = "closeAd";
Obj.onclick = function(){
this.parentNode.style.display = "none";
}
adElement[i].appendChild(Obj);
}
})();
/* 同步 */
window.onload = function(){
var adElement = document.getElementsByTagName("ins");
var re = /aswift_(.*?)_expand/;
var Obj;
for(var i = 0, adLen=adElement.length; i < adLen; i++){
if(!re.test(adElement[i].id)) continue;
Obj = document.createElement("img");
Obj.className = "closeAd";
Obj.onclick = function(){
this.parentNode.style.display = "none";
}
adElement[i].appendChild(Obj);
}
};
加上一小段CSS
.closeAd{
position: absolute;
top: 0px;
left: 0px;
width: 20px;
height: 20px;
cursor: pointer;
background-image: 圖片;
}
就可以讓使用者關閉(隱藏)廣告啦
我經營的論壇: 台論之星
塵世中一位載浮載沉之小小工程師