jquery-从加载的页面隐藏父div

jquery-从加载的页面隐藏父div,jquery,html,load,hide,parent-child,Jquery,Html,Load,Hide,Parent Child,所以,我需要从主页关闭一个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(functio

所以,我需要从主页关闭一个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>

显示1
表演2
$('#ShowPage1')。单击(函数(){
$(“#DIV1”).load(“test1.html”);
$(“#DIV1”).show();
返回false;
});
$('#ShowPage2')。单击(函数(){
$(“#DIV1”).load(“test2.html”);
$(“#DIV1”).show();
返回false;
});
这是test1.html

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

<script type="text/javascript">
$('#HideDIV1').click(function(){
   $("#DIV1").hide();
   return false;
});
</script>
HideDiv
$('#HideDIV1')。单击(函数(){
$(“#DIV1”).hide();
返回false;
});
第一部分工作很好,它加载test1.html并显示DIV1。 Bun当我试图从test1.html中隐藏它时,#HideDIV1。。。只是不起作用


请提供帮助。

尝试将hideDIV1的单击事件委派给其父项。假设您将
hideDIV1
附加到
#DIV1

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

这将出现在主页的脚本中。

test1.html中的javascript是否实际运行?尝试在其中放入alert()或console.log()以将其签出。是的,使用alert()workTry
$('#HideDIV1')。在('click',function(){$('#DIV1')。hide();return false;})上不,它不工作如果我把所有东西都放在主页上,在DIV1之后,当我按下#HideDiv1时,它工作了,但是如果#HiveDiv1它在加载的页面test1.html中,它不工作了,或者说,错过了HideDiv1的选择器。检查更新的答案(添加到选择器中)