2016-03-04 108 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進行了嘗試,並且不工作。 –