jQuery(document).ready(function (e) {
focus();
var listener = addEventListener('blur', function() {
if(document.activeElement === document.getElementById('my_iFrame')) {
console.log("clicked!");
}
removeEventListener(listener);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe src="http://example.com" id="my_iFrame"></iframe>
此代码只计算一次点击。如何统计更多点击?
谢谢!
最佳答案
@benjarwar 在他的两条评论中都是正确的,但是我认为以下解决方案比另一篇文章中建议的解决方案更优雅,但作为一个超过 1 年不活跃的帖子,我将在此处发布解决方案。
只要您可以访问 iframe 和父框架代码,这就可以跨域工作。
假设您也会在父级和 iframe 中使用 jQuery!
在您添加的 iframe 中:
$("html").on("click",function(){
parent.postMessage("click", "*");
});
在您添加的父项中:
window.addEventListener("message", function(event) {
if (event.data=="click")
//above if only added to be sure you are not getting some other messages post to you parent page.
{ console.log("clicked!"); }
});
关于javascript - 计算 iframe 上的点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237217/