如何检查 div 中是否有任何内容可见
我的页面上有一个脚本,它将广告网络中的内容加载到 div 中。该广告网络的填充率为 80%,即它仅在 80% 的时间内投放广告,另外 20% 的时间内不展示广告。遗憾的是,广告网络没有向我提供回调来告诉我它何时没有投放广告。因此,有时此 div 会显示广告,有时则显示空白。
这在我的网站上看起来很可怕,让我成为一只悲伤的熊猫。有没有办法检查 div 内是否有内容可见?如果我知道这一点,我就会知道何时显示 div,何时隐藏它。
这听起来像是一个很难解决的问题,但我想看看是否有任何解决方案。交给你们了,大师们。
I have a script on my page which loads content from an ad network into a div. This ad network has an 80% fill rate i.e. it only serves an ad 80% of the time, the other 20% of the time it doesn't show an ad. Sadly, the ad network doesn't provide me with a callback to tell me when it hasn't served an ad. As a consequence, sometimes this div shows an ad, and other times it's blank.
This looks horrendous on my website, and makes me a sad panda. Is there a way to check if any content is visible inside a div? If I knew this, I would know when to show the div, and when to hide it.
This sounds like a hard problem to solve, but I wanted to see if there's any solution. On to you, gurus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想说最好的解决方案是寻找更可靠的广告网络。但是,您可以通过使用 jQuery 对象的
length
属性来检查任何 DOM 元素是否存在。例如:这只是一个通用示例,因为我不知道广告标记的结构,也不知道有时会缺少哪些元素,但重点是使用
length
属性来查看选择器有多少个元素匹配。如果找不到该元素,该值将为零,您可以使用它来采取一些操作。I'd say the best solution is to look for a more reliable ad network. However, you can check for the existence of any DOM element by using the
length
property on a jQuery object. For example:That's just a generic example since I don't know the structure of your ad markup or what element is sometimes missing, but the point is to use the
length
property to see how many elements the selector matches. If it doesn't find the element, the value will be zero and you can use this to take some action.假设 Div 可见性不是由广告设置的。这是 sudo 代码。
1.) 使用 id 或名称将变量分配给 div
2.) 将 div 内部文本放入变量
3.) 检查内部文本 div 是否为空
4.) 根据 DIV 之间的文本更改 DIV 的可见性标签。
Here you go Assuming that the Div visibility is not set by the ad. This is sudo-code.
1.) Assign a variable to the div using the id or name
2.) Place the div inner text into a variable
3.) Check the inner Text div if it is null
4.) Change Visibility of DIV based on text being between DIV tags.
拥有一些实际的标记会有所帮助,但如果我没有正确理解你的意思,你通常有一个空的 div,然后有时有一个 iframe 或嵌套的 iframe 或类似的可怕的东西。
所以我会使用两个并发解决方案:首先,最简单的 jQuery 解决方案是:(
将
#ad-container div
替换为您使用的任何内容。)同时,当这不起作用时(讨厌的 iframe 之类的东西),使用 CSS 提供备份背景图像,也许可以推销自己,或者只是让空间看起来更好。只是基本的,就像这样:
当广告投放时,图像将被覆盖。如果没有,备份将为您提供帮助。
Having some actual markup would help, but if I'm reading you correctly, you most often have an empty div and then sometimes have an iframe or nested iframes or something similarly horrendous.
So I'd use two concurrent solutions: first, the simplest jQuery solution would be:
(Replace
#ad-container div
with whatever you use.)Meanwhile, for the times when that won't work (nasty iframes and stuff), use CSS to provide a backup background image, maybe promoting yourself or just making the space look better. Just basic, like so:
When the ad is served, the image will be covered. When it's not, the backup'll be there for you.
广告投放成功后,IFrame 是否有图片标签?
Does the IFrame has an image tag when the ad is served successfully?