jquery - 从加载的页面隐藏父级 div
jquery - Hide a parent div , from loaded page
所以,我需要从主页关闭一个 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 中隐藏它时,...只是不起作用。
请帮忙。
尝试将 hideDIV1
的点击事件委托给其父级。假设您将 hideDIV1
附加到 #DIV1
:
$('#DIV1').on('click','#hideDIV1',function(){
$("#DIV1").hide();
return false;
});
这将放在主页的脚本中。
所以,我需要从主页关闭一个 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 中隐藏它时,...只是不起作用。
请帮忙。
尝试将 hideDIV1
的点击事件委托给其父级。假设您将 hideDIV1
附加到 #DIV1
:
$('#DIV1').on('click','#hideDIV1',function(){
$("#DIV1").hide();
return false;
});
这将放在主页的脚本中。