如何在点击iframe时关闭它?

时间:2015-03-19 07:39:02

标签: javascript jquery html iframe

下面的脚本会检测到iframe上的点击并返回一条消息 我希望iframe在检测到点击时自动关闭。



			$(window).on('blur',function(e) {    
				if($(this).data('mouseIn') != 'yes')return;
				$('iframe').filter(function(){
					return $(this).data('mouseIn') == 'yes';
				}).trigger('iframeclick');    
			});

			$(window).mouseenter(function(){
				$(this).data('mouseIn', 'yes');
			}).mouseleave(function(){
				$(this).data('mouseIn', 'no');
			});

			$('iframe').mouseenter(function(){
				$(this).data('mouseIn', 'yes');
				$(window).data('mouseIn', 'yes');
			}).mouseleave(function(){
				$(this).data('mouseIn', null);
			});

			$('iframe').on('iframeclick', function(){
				console.log('Clicked inside iframe');
				$('#result').text('Clicked inside iframe'); 
			});
			$(window).on('click', function(){
				console.log('Clicked inside window');
				$('#result').text('Clicked inside window'); 
			}).blur(function(){
				console.log('window blur');
			});

			$('<input type="text" style="position:absolute;opacity:0;height:0px;width:0px;"/>').appendTo(document.body).blur(function(){
					$(window).trigger('blur');
				}).focus();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<iframe width="350" height="250" src="http://omarjackman.com/about/" ></iframe>
<div id="result"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

$('iframe').on('iframeclick', function(){
            $(this).hide() // Or remove() if you dont want it anymore in the DOM
            $('#result').text('Clicked inside iframe'); 
        });