【问题标题】:Remove a div after an element in a iframe inside that div is clicked单击该 div 内的 iframe 中的元素后删除该 div
【发布时间】:2016-05-17 18:07:39
【问题描述】:

我在 div 中有一个 AdSense,并希望保护它免受无效流量的影响。 有人点击广告后,应将其隐藏,一段时间后不再显示。

HTML:

<div id="codeWrapper"> <div id="firstCode">Adsense code here</div> </div>

CSS:

#codeWrapper { position: relative;}
#firstCode { position: absolute; width: 336px; height: 280px; z-index: 999;}

JS:

$("#firstCode").click(function () {
        var date = new Date();
        var h = "24";
        date.setTime(date.getTime() + (h * 60 * 60 * 1000));
        Cookies.set('clicked', 'true', {expires: date, path: '/'});

        $(this).parent().remove();
});

我使用 javascript cookie 来存储点击广告的日期并设置到期时间。

控制台中没有错误,但不知何故它不起作用。

【问题讨论】:

  • 如果你console.log($(this).parent()); 你得到了什么? codeWrapper?
  • 当广告在新标签页中打开时,它不会记录任何内容。当广告开始重定向到另一个网址时也是如此。
  • 如果我删除 iframe 并单击,这就是我得到的:[div#codeWrapper, selector: "", prevObject: a.fn.init[1], context: div#firstCode]

标签: javascript jquery css iframe cookies


【解决方案1】:

如果 iframe 具有不同的服务器,则实际浏览器不允许脚本操纵 iframe 内部内容。因此,您需要检查 iframe url 是否与当前 url 具有相同的服务器。

【讨论】:

  • OP不想对iFrame的内容做任何事情,他想删除firstCode的父元素codeWrapper。无论如何,这是评论,而不是答案。
  • 他的函数返回Uncaught ReferenceError: Cookies is not defined,但在删除cookie的函数时效果很好。也许他需要调试这个对象。 jsfiddle.net/alexndreazevedo/s9u25yfm
  • Cookies 没有定义,因为你没有实现资源:github.com/js-cookie/js-cookie
  • 如果你使用文字,它可以工作,但当它是 iframe 中的广告时,它就不起作用
  • 实际上,您需要验证广告 iframe 是否在您页面的同一服务器 url 上。由于浏览器的限制,无法从另一台服务器操作 iframe 的内部内容。请检查一下,我们可以尝试其他解决方案。
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 2013-11-03
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2019-11-05
  • 2016-04-19
相关资源
最近更新 更多