Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery、Javascript或Ajax:单击iframe关闭它_Javascript_Jquery_Html_Ajax - Fatal编程技术网

Jquery、Javascript或Ajax:单击iframe关闭它

Jquery、Javascript或Ajax:单击iframe关闭它,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,每当有人单击iframe中的任何链接时,我都会关闭iframe和div 我知道这是可能的,我见过一些网站这样做。 只是不记得这些网站的网址 我不久前在一个站点上复制了这段代码,并用于在iframe中定位单击 var iframe = document.getElementById('the_iframe'); iframe.contentWindow.document.body.addEventListener('click', function(event {

每当有人单击iframe中的任何链接时,我都会关闭iframe和div

我知道这是可能的,我见过一些网站这样做。 只是不记得这些网站的网址

我不久前在一个站点上复制了这段代码,并用于在iframe中定位单击

    var iframe = document.getElementById('the_iframe');
    iframe.contentWindow.document.body.addEventListener('click', function(event {
            //check if target is an anchor tag
            if (e.target.tagName === 'A') {
                    //remove iframe
                    iframe.parent.removeChild(iframe);
            }
    });
不知道如何使用脚本

$.enableFrameClick = function(){
var decoy = $('<a href="#"/>').css({
    position: 'absolute',
    right:0,
    top:0,
    width:1,
    opacity:0
}).click(function(e){ return false }).appendTo(document.body);

$(window).blur(function(){
    if ($.inFrame){
        $('#'+$.inFrame).trigger('click');
        setTimeout(function(){
            decoy.focus();
        }, 10);
    }
});

var ids = +(new Date);
$('iframe').each(function(){
    if (!this.id){
        this.id = ++ids;
    }
    $(this).hover(function(){
        $.inFrame = this.id;
    }, function(){
        $.inFrame = null;
    })
});
};

$('#mha iframe').bind('click', function(){
    $.ifrm(1);
    //log(['[',++i,']','clicked', this.id].join(' '));
});

function log(m){
    //$('#log').text(m);
    //console.log(m);
}

var i = 0;

$.ifrm = function(r){
    $.get("http://www.example.com/"+r, function(html,status){
        $('#mha iframe').attr("src",html);
        if(r == 1)
            html = '';
        if(html == '')
            $('#fanback').remove();
    });
};

$.enableFrameClick();
$.ifrm(0);

如果您只想让div“消失”,可以尝试使用以下命令:

 $("#button_id").click(function(){
     $("#div_id").hide();
 });

您不能在iFrame内触发类似鼠标单击的事件,但仍有一些方法可以这样做:

我希望能帮助你
我认为工作也是跨域的,但我不确定iframe节点是否有contentWindow属性,即iframe的窗口对象

    var iframe = document.getElementById('the_iframe');
    iframe.contentWindow.document.body.addEventListener('click', function(event {
            //check if target is an anchor tag
            if (e.target.tagName === 'A') {
                    //remove iframe
                    iframe.parent.removeChild(iframe);
            }
    });
请注意,仅当iframe源位于同一域、端口和协议上时,才能将事件附加到iframe内的节点


请参阅以获取可用演示。

是否关闭iframe?你是说从DOM中隐藏/删除?对不起!iframe所在的div。lshetty不允许访问iframe的内容,除非它位于您的服务器上。或者使用脚本设置相应的访问控制-*标题以更新问题。你能告诉我它是怎么工作的吗@埃蒂斯