简体   繁体   English

使用onclick在其他html文件中显示隐藏的div

[英]With onclick show hidden div in other html file

I use Javascript and 'onclick' to show hidden div's in a large database, when I use onclick it shows as a popup. 我使用Javascript和“ onclick”在大型数据库中显示隐藏的div,当我使用onclick时它显示为弹出窗口。 It works fine in the same html-file, but I cannot get an id openend in another html file. 在同一个html文件中它可以正常工作,但是我无法在另一个html文件中获得id openend。 How can I do this? 我怎样才能做到这一点?

I use this javascript code (sh = show): 我使用以下javascript代码(sh = show):

 var divState = {}; function sh(id) {
if (document.getElementById) {
    var divid = document.getElementById(id);
divState[id] = (divState[id]) ? false : true;
 //close others
 for (var div in divState){
    if (divState[div] && div != id){
        document.getElementById(div).style.visibility = 'hidden';
        divState[div] = false;
    }
}
divid.style.visibility = (divid.style.visibility == 'visible' ? 'hidden' : 'visible'); } }

And I call the item with this code (id=1): 然后用以下代码(id = 1)调用该项目:

 <a class=w onclick="sh('1');">asdfasdf asdasdfds</a>

I tried <a onclick="window.location.href='../A/index.html?id=0174' "> but it does not "open" or rather show id=0174, only opens index.html in the A-directory. 我尝试了<a onclick="window.location.href='../A/index.html?id=0174' ">但是它不会“打开”或显示id = 0174,而是仅在A-目录。 How can I make id 0174 in A/index.html visible when clicked on some item in E/index.html? 单击E / index.html中的某些项目后,如何使A / index.html中的ID 0174可见?

Many thanks! 非常感谢!

You can use the :target CSS pseudo-class to show or hide elements with the id specified as the hash in the url. 您可以使用:target CSS伪类来显示或隐藏ID为URL中的哈希值的元素。

For example: <a href="../A/index.html#0174">Link to div #0174 on other page</a> 例如: <a href="../A/index.html#0174">Link to div #0174 on other page</a>

 .show-when-target { visibility: hidden; } .show-when-target:target { visibility: visible; } 
 <div id="1" class="show-when-target"> Hello from div 1! </div> <div id="2" class="show-when-target"> Hello from div 2! </div> <div id="3" class="show-when-target"> Hello from div 3! </div> <a href="#1">Show Div 1</a> <a href="#2">Show Div 2</a> <a href="#3">Show Div 3</a> 

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

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