本页 https:/www.perlentaucher.destaticyl-raw.html 开头有一个div,我想关闭它。
如果我运行
parent.document.getElementById('div-gpt-ad-1407836088099-0').style.display='none';
在谷歌浏览器的控制台,div将关闭。然而,如果我把这段代码放在一个google广告管理器的第三方创意里面。我总是得到错误 "Uncaught TypeError: Cannot read property 'style' of null"(无法读取null属性)。
function addNewStyle(newStyle) {
var styleElement = document.getElementById('styles_js');
if (!styleElement) {
styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.id = 'styles_js';
document.getElementsByTagName('head')[0].appendChild(styleElement);
}
styleElement.appendChild(document.createTextNode(newStyle));
}
addNewStyle('#div-gpt-ad-1407836088099-0 {display:none !important;}')
问题是,每个新的广告单元的创意连接到一个是必要的添加父。所以最后就变成了这样。
parent.parent.parent.parent.document.getElementById('div-gpt-ad-1407836088099-0').style.display='none';