使用jquery从iframe获取click事件

时间:2011-05-05 19:07:37

标签: jquery iframe click

如何在我的网页中从iframe div获取点击事件,我在哪里使用jquery放置iframe?

2 个答案:

答案 0 :(得分:4)

使用jQuery contents与iframe内容进行互动。

var frame = $('<iframe />').appendTo('body');  // create the iframe and append it to the body
var frameBody = frame.contents().find('body'); // grab the body node
frameBody.append('<div />');                   // add a div
frameBody.find('div');                         // retrieve the div

如果您要从网址加载iframe,请确保遵守同源策略,并等待iframe加载:

frame.load(function(){
    var frameBody = frame.contents().find('body');
    frameBody.find('div').click(function(){
        // here is the iframe div click callback
    }); 
});

答案 1 :(得分:0)

这可能会帮助你@Valisimo。这仅适用于内部域。

$(document).ready(function(e) {
    // here , I write <h2> HTML tags to try.

    $('#frame').contents().find('h2').click(function(){
        $('#frame').contents().find('h2').toggle(1000);
    })
});
相关问题