javascript - jquery 处理来自外部 iframe 的事件

标签 javascript

我的 IFRAME 加载了外部内容(ibnlive.in.com),我无法处理该框架中的点击事件。

这是我的代码,

var iframeDoc = $('#bookcontentHeight').contents().get(0);
        // Bind event to iframe document
        $(iframeDoc).bind('mouseup', function(e) {                       
                e.preventDefault();                 
                $("#custom-menu").css({ top: e.pageY + "px", left: e.pageX + "px" }).show(100);
        });

但是我可以处理 IFRAME 中加载的本地数据,

<iframe src="books/1.xhtml" width="100%" id="bookcontentHeight" frameborder="0" marginheight="0" marginwidth="0" > </iframe>    

我如何处理从外部源加载的点击事件。

最佳答案

您需要将事件处理程序附加到略有不同的对象。尝试:

var targetWindow = iframeDoc.contentWindow || iframeDoc.contentDocument;
if (targetWindow.document) {
    var targetDocument = targetWindow.document;

    $(targetDocument.body).bind("mouseup", function(){
        // event handler
    });
}

关于javascript - jquery 处理来自外部 iframe 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445982/

相关文章:

javascript - react : Cross origin requests on loading file without local server

javascript - 我怎样才能让这个 javascript 函数发出一个换行符

php - API 错误代码 : 100 facebook OpenGraph

javascript - 无法读取属性 'getBounds' 。使用 addListenerOnce

javascript - 比较条件对

javascript - window.close 不适用于 IE

javascript - 使用javascript/jquery跟踪浏览器发送的http请求

javascript - NodeJS 模块初始化

javascript - 使用纯 JavaScript 创建对此 HTML 元素的引用的最有效方法是什么?

javascript - 为什么 bootstrap datepicker 显示默认日期为 00/14/2014?