2016-03-04 185 views
0

所以,我需要从主页面关闭div,并从加载的页面按钮。jquery - 从加载页面隐藏父div,

这是主要的网页:

<div id="DIV1" style="display:none;"></div> 
<div id="ShowPage1">Show 1</div> 
<div id="ShowPage2">Show 2</div> 

<script type="text/javascript"> 
$('#ShowPage1').click(function(){ 
    $("#DIV1").load("test1.html"); 
    $("#DIV1").show(); 
    return false; 
}); 
$('#ShowPage2').click(function(){ 
    $("#DIV1").load("test2.html"); 
    $("#DIV1").show(); 
    return false; 
}); 
</script> 

这是test1.html

<div id="HideDIV1">HideDiv</div> 

<script type="text/javascript"> 
$('#HideDIV1').click(function(){ 
    $("#DIV1").hide(); 
    return false; 
}); 
</script> 

第一部分,很好地工作,将其加载test1.html并显示DIV1。 发布时,我尝试从test1.html隐藏它#HideDIV1,...只是不工作。

请帮忙。

+0

是否来自test1.html的javascript实际运行?尝试在其中放入一个alert()或console.log()来检查它。 – Sebastianb

+0

是的,带有alert()工作 –

+0

尝试''('#HideDIV1')on('click',function(){$('#DIV1')。hide(); return false;});' – 2016-03-04 18:46:06

回答

0

尝试将hideDIV1的点击事件委托给其父项。比方说,你追加hideDIV1#DIV1

$('#DIV1').on('click','#hideDIV1',function(){ 
    $("#DIV1").hide(); 
     return false; 
}); 

这将走在主网页的脚本。

+0

仍然无法运行 –

+0

对不起,错过了hideDIV1的选择器。检查更新后的答案(在选择器中添加#) – Sebastianb

+0

我已经用#对div的id进行了尝试,并且不工作。 –