2012-08-23 158 views
3

我有兩頁。一個是顯示隱藏div從另一頁onclick

<html> 
<head> 
<script language="javascript"> 
    function toggleDiv(divid){ 
    if(document.getElementById(divid).style.display == 'none'){ 
    document.getElementById(divid).style.display = 'block'; 
}else{ 
    document.getElementById(divid).style.display = 'none'; 
} 
} 
</script> 
</head> 
<body> 

<a name="div1" href="javascript:;" onmousedown="toggleDiv('div1');"><p><b>Section 1</b>  </p></a> 
<div id="div1" style="display:none"> 
Content for section 1. 
</div> 
<a name="div2" href="javascript:;" onmousedown="toggleDiv('div2');"><p><b>Section 2</b></p></a> 
<div id="div2" style="display:none"> 
Content for section 2. 
</div> 
</body> 
</html> 

在其他的網頁我有:

<html> 
<head> 
<title>Test</title>  
</script> 
</head> 
<body> 
<a href="main.html#iv2">Section 2</a> 
</div> 

<script type="text/javascript"> 
if (location.hash.length > 1) 
{ 
    toggleDiv(location.hash.substring(1)); 
} 
</script> 
</body> 
</html> 

我喜歡這裏實現的,當我點擊「第2」的第二頁上,主頁上會用「DIV2打開「內容顯示。上面的代碼對我不起作用。

回答

1

d中缺少ID

<a href="main.html#div2">Section 2</a> 

和這個腳本應該是在第1頁

<script type="text/javascript"> 
if (location.hash.length > 1) 
{ 
    toggleDiv(location.hash.substring(1)); 
} 
</script> 

希望這將有助於

這是你第1頁(主要的完整代碼。 html)

<html> 
<head> 

</head> 
<body> 

<a name="div1" href="javascript:;" onmousedown="toggleDiv('div1');"><p><b>Section 1</b>  </p></a> 
<div id="div1" style="display:none"> 
Content for section 1. 
</div> 
<a name="div2" href="javascript:;" onmousedown="toggleDiv('div2');"><p><b>Section 2</b></p></a> 
<div id="div2" style="display:none"> 
Content for section 2. 
</div> 
</body> 

<script type="text/javascript"> 
if (location.hash.length > 1) 
{ 
    toggleDiv(location.hash.substring(1)); 
} 
function toggleDiv(divid){ 
    alert(divid); 
    if(document.getElementById(divid).style.display == 'none'){ 
    document.getElementById(divid).style.display = 'block'; 
}else{ 
    document.getElementById(divid).style.display = 'none'; 
} 
} 
</script> 
</html> 
+0

謝謝。我會試試這個,讓你知道 –

0

你不能調用JavaScript函數這樣的...這是行不通的......你需要調用在礦井頁面的onload事件的功能...像

<body onload="toggleDiv('div2')"> 
</body> 

否則

打開彈出式並使用「開窗器」屬性。看到這個鏈接http://www.w3schools.com/jsref/prop_win_opener.asp