<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script src="http://www.pc120xz.cn/scripts/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="top" style="height:99px; border:1px solid #933; background-color:#999999;">
</div>
<div id="divmain">
<iframe name="contentFrame" id="contentFrame" width="100%" height="500" allowtransparency="true" scrolling="no" border="0" frameborder="0" src="main.html" ></iframe>
</div>
<div id="bottom" style="height:99px; border:#06F solid 1px; background-color:#999999;">此處顯示 id "bottom" 的內容</div>
<script type="text/javascript">
$("#contentFrame").load(function(){
var mainheight = $(this).contents().find("body").height();
$(this).height(mainheight);
});
$(document).ready(function(){
$("#ListShow").bind("click",function(){
$('#Main_pl1').animate({height:'show', opacity: 'show'}, 'normal',function(){
$('#Main_pl1').show();
});
$('#ListShow').attr('style','display:none');
$('#ListHide').attr('style','display:block');
return false;
});
$("#ListHide").bind("click",function(){
$('#Main_pl1').animate({height:'hide', opacity: 'hide'}, 'normal',function(){
$('#Main_pl1').hide();
});
$('#ListShow').attr('style','display:block');
$('#ListHide').attr('style','display:none');
});
$(document).bind("click",function(event){
if($(event.target).isChildOf("#Main_pl1") == false){
$('#Main_pl1').animate({height:'hide', opacity: 'hide'}, 'normal',function(){
$('#Main_pl1').hide();
});
$('#ListShow').attr('style','display:block');
$('#ListHide').attr('style','display:none');
}
});
$.fn.isChildAndSelfOf = function(b){
return (this.closest(b).length > 0);
};
$.fn.isChildOf = function(b){
return (this.parents(b).length > 0);
};
});
</script>
<div id="MainA1" style="position:fixed;left:0px;bottom:2px;_position:absolute;">
<div id="Main_pl1" style="display:none;">
<div class="Main_pl1m" style="width:400px; height:200px; background:#666666;">點擊此DIV外任意地方隱藏此DIV 或點擊iframe子頁面中隱藏 父頁面DIV</div>
</div>
<div id="Main_pm1" style="width:400px; height:120px;">
<div class="Main_pm1m overz" style="border:#9CC solid 1px;">
<span title="打開列表" id="ListShow" style="display:block;">測試這個</span>
<span title="關閉列表" id="ListHide" style="display:none;">測試這個</span>
</div>
</div>
</div>在同一頁面可以 但DIV浮動在iframe上時 失效
</body>
</html>
之前不是回過你了?iframe和父頁是不同的區域,點擊iframe是無法響應父頁的click事件的,用透明的層遮蓋住iframe(跨域)或者沒有跨域的情況下給iframe添加click事件隱藏div
$("#contentFrame").load(function () {
var mainheight = $(this).contents().find("body").height();
$(this).height(mainheight);
//////////////////////////////增加下面的代碼觸發父頁綁定的click事件
$(this).contents().click(function () {
$(parent.document).trigger('click');
});
//////////////////////////////
});
或者透明層遮蓋住iframe
<div id="divmain" style="position:relative">
<div style="position:absolute;left:0px;top:0px;width:100%;height:100%;background:#000;filter:alpha(opacity=0);opacity:0"></div>
<iframe name="contentFrame" id="contentFrame" width="100%" height="500" allowtransparency="true" scrolling="no" border="0" frameborder="0" src="http://www.baidu.com" ></iframe>
</div>