Jquery capture Click on Iframe

走远了吗. 提交于 2019-12-18 06:59:26

问题


So I have an Iframe on my page like

<iframe width="640" height="360" frameborder="0" allowfullscreen="" src="http://www.youtube.com/xxxxxx">

I was wondering how I could capture the click event on this. The Iframe's do not have an id or class set on them


回答1:


You can not interact with iframes which are not under the same domain. This is always prevented by the browser's policy.

See Detect Click into Iframe using JavaScript




回答2:


I ran into a situation where I had to track clicks on a social media button pulled in through an iframe. A new window would be opened when the button was clicked. Here was my solution:

var iframeClick = function () {
    var isOverIframe = false,
    windowLostBlur = function () {
        if (isOverIframe === true) {
            // DO STUFF
            isOverIframe = false;
        }
    };
    jQuery(window).focus();
    jQuery('#iframe').mouseenter(function(){
        isOverIframe = true;
        console.log(isOverIframe);
    });
    jQuery('#iframe').mouseleave(function(){
        isOverIframe = false;
        console.log(isOverIframe);
    });
    jQuery(window).blur(function () {
        windowLostBlur();
    });
};
iframeClick();


来源:https://stackoverflow.com/questions/15557155/jquery-capture-click-on-iframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!