检测对包含 Javascript 的 DIV 元素的点击?
如何检测从
触发的 javascript 的点击?例如,我在页面上的 3 个不同
中有 3 个 adSense 广告,我希望在点击广告时检测并触发操作。当
为空或带有任何其他元素时,很容易检测到对它的点击;但如何检测 adSense 广告(代码)的点击次数?How can you detect clicks on javascript which is fired from a <DIV>
?
For example I have 3 adSense ads in 3 different <DIV>
s on a page, and I want to detect and trigger an operation when an ad is clicked.
It is easy to detect clicks on<DIV>
when it is empty, or with any other element; but how to detect clicks on adSense ad (code)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,Adsense 广告加载在 iframe 元素中,因此访问它们将违反同源政策。这意味着您无法检测 iframe 中指向外部 URL 的点击,因此无法完成此操作。
As far as I'm aware, Adsense ads are loaded in an iframe element so accessing them would be violating the same origin policy. This means you can't detect clicks in an iframe pointing to an external URL, so it can't be done.
你使用像 jQuery 这样的框架吗?如果是这样,您可以将点击处理程序添加到 div 的子级:
are you using any frameworks like jQuery? if so, you could add a click handler to a child of a div:
如果我真的需要实现这样的目标,我会欺骗用户一点。
不要尝试点击 iframe,而是制作一个覆盖 div 并将其放置在 iframe 上方。给它附加点击事件,当div被点击时,隐藏它。
它会让用户感觉他点击了链接,但它没有正常工作。他第二次点击它就已经起作用了,因为覆盖层被隐藏了。
示例代码(仅用于解释目的):
If I would really need to achieve something like this, I would cheat a bit with the user.
Instead of trying to get click on iframe, make an overlay div and place it above iframe. Attach click event to it, and when div is clicked, hide it.
It will give the user feeling that he clicked on link, but it did not worked correctly. The second time he clicks it will already worked, cause overlay is hidden.
An example code (just for explanation purpose):