当谷歌广告不显示时如何以编程方式折叠空 div 中的空间 [英] How to programmatically collapse space in empty div when google ad does not show

查看:38
本文介绍了当谷歌广告不显示时如何以编程方式折叠空 div 中的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以编程方式折叠 Google 广告未显示时产生的空白区域?如果是这样,我很想看到一个说明性的例子.

四处搜索使我找到了这个 Google 官方资源来完成正是我所问的.但是,这与 DFP 广告管理系统有关,不幸的是,它是一个单独的产品.我很想知道如何为 AdSense 处理这个问题 - 我的一些用户目前正在盯着空白处.

<小时>

以防万一,以下是 Google AdSense 提供的示例广告片段(我已将其居中对齐):

 

<ins class="adsbygoogle"样式=显示:块"数据广告客户端="ca-pub-0000000000000000"data-ad-slot="0044031319"data-ad-format="auto"></ins><脚本>(adsbygoogle = window.adsbygoogle || []).push({});

解决方案

我知道这已经过时了,但因为我现在一直在处理这个问题.在 jQuery 中执行此操作的一个足够简单的方法是检查所有具有 adsbygoogle 类且内部没有子元素的元素.

这将选择具有该类的所有元素并隐藏它们,有效地折叠它们.

$(".adsbygoogle:empty").hide();

你也可以用它做很多其他的事情,比如如果它在一个 div 中并且你也需要隐藏它,使用 $(".adsbygoogle:empty").parent().hide() 进一步折叠它.

我相信这可以很容易地用 vanilla javascript 完成.我建议在 DOM 加载后运行这行代码,甚至等待 10 秒钟,看看谷歌是否填充了广告.

Is there any way to programmatically collapse the empty space that results when a google ad does not show? If so, I would love to see an illustrative example of the same.

Searching around has led me to this official Google resource for accomplishing exactly what I've asked. However, that pertains to Doubleclick for Publishers, which is unfortunately a separate product. I'm pining to know how to handle this for AdSense - some of my users are staring at empty spaces at the moment.


In case it matters, here's an example ad snippet provided by Google AdSense (which I've center-aligned):

    <div style="text-align:center">
        <ins class="adsbygoogle"
             style="display:block"
             data-ad-client="ca-pub-0000000000000000"
             data-ad-slot="0044031319"
             data-ad-format="auto"></ins>
        <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
    </div>

解决方案

I know this is old, but since I've been dealing with this now. A simple enough way to do this in jQuery is to check for all elements with the class adsbygoogle that have no child inside.

This selects all the elements with that class and hides them, effectively collapsing them.

$(".adsbygoogle:empty").hide();

You can do a lot of other stuff with it too, like if it's in a div and you need to hide that too, use the $(".adsbygoogle:empty").parent().hide() to collapse it further.

I'm sure this can be done with vanilla javascript as easily. I suggest to run this line of code after the DOM has loaded and even wait like 10 seconds just to see if google populates the ads.

这篇关于当谷歌广告不显示时如何以编程方式折叠空 div 中的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆