拡張しようとしているdivが崩壊している他のdiv内にある場合、子供が示すために子供を拡張する前にコンテナdivを拡張する必要があります。したがって、少なくともヘッドコードのトグル関数のようなものを呼び出す必要があります。たとえば
window.onload = function() {
var hash = window.location.hash; // would be "#div1" or something
if(hash != "") {
toggle('CPNRE', this);/*this is an example the CPNRE value needs to be figured out*/
var id = hash.substr(1); // get rid of #
document.getElementById(id).style.display = 'block';
}
}
これは、トグルを呼び出すためにコンテナdivを把握する必要があるため、拡張するためにコンテナdivを把握する必要があるため、ほぼ完全なソリューションではありません。
編集
純粋なJSで、あなたがやろうとしていることを達成する1つの方法は、次のことをすることです。
if(hash != "") {
var id = hash.substr(1); // get rid of #
document.getElementById(id).style.display = 'block';
document.getElementById(id).parentNode.parentNode.parentNode.style.display = 'block';
document.getElementById(id).parentNode.parentNode.style.display = 'block';
document.getElementById(id).parentNode.style.display = 'block';
document.getElementById(id).style.display='block';
}
しかし、これはあなたがあなたの質問で提供した特定のレイアウトに関連しています。
より良い、より一般的な解決策は、たとえば、クラスの値に基づいて特定の親が見つかるまで親を反復することです。
HTML
/*add class value like faq-section to each div section */
<div class="FAA faq-section" id="faq1">
JS
....
if(hash != "") {
var id= hash.substr(1);
var pNode = document.getElementById(id).parentNode;
var endLoop=false;
while(!endLoop){
if(pNode.className&&pNode.className.indexOf('faq-section')!=-1){
endLoop=true;
};
pNode.style.display='block';
pNode=pNode.parentNode;
}
document.getElementById(id).style.display='block';
}