简体   繁体   English

从另一个页面显示隐藏的div onclick

[英]show hidden div onclick from another page

I have Two pages. 我有两页。 One is 一个是

<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>

On other page I have: 在其他页面上我有:

<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>

What I like to achieve here is, when I click on "Section 2" on the second page, main page will open with "div2" content displayed. 我想在这里实现的是,当我点击第二页上的“第2节”时,将打开主页面,显示“div2”内容。 The above code doesn't work for me. 上面的代码对我不起作用。

d is missing in id id中缺少d

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

and this script should be in page 1 这个脚本应该在第1页

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

Hope this will help 希望这会有所帮助

this is complete code of your page 1 (main.html) 这是您的第1页的完整代码(main.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>

You cannot call Javascript function Like that...That won't work...you need to call the function inthe onLoad Event of the Page...like 你不能调用Javascript函数那样......那不行 - 你需要在页面的onLoad事件中调用函数...喜欢

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

Else 其他

Open The Popup and Use "Window opener" property.See this link http://www.w3schools.com/jsref/prop_win_opener.asp 打开Popup并使用“Window opener”属性。 查看此链接http://www.w3schools.com/jsref/prop_win_opener.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM