简体   繁体   中英

JQuery HideAllShowOne + Link to open DIV

I am using this JQuery only using HideAllShowOne:

<script type="text/javascript" language="JavaScript"><!--
function HideContent(d) {
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
document.getElementById(d).style.display = "block";
}
function HideAllShowOne(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}
function HideAllShowOne(d) {
// Between the quotation marks, list the id values of each div.

var IDvaluesOfEachDiv = "1 2 3 4 5 6 7 8 9 10 11 12 13 14";

//-------------------------------------------------------------
IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/[,\s"']/g," ");
IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/^\s*/,"");
IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/\s*$/,"");
IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/  +/g," ");
var IDlist = IDvaluesOfEachDiv.split(" ");
for(var i=0; i<IDlist.length; i++) { HideContent(IDlist[i]); }
ShowContent(d);
}
//--></script>

With this HTML

<div class="heading marg_none">
    <h5><a href="javascript:HideAllShowOne('2')">Step 2. Adding Services</a></h5>
</div>
<div id="2" style="display: none;">         
     <p>
         Watch the video for full details.            
     </p>
</div>

This works fine for opening each section in a FAQ style page on my website but what I want be able to link to each question from somewhere else on the site and have it automatically scroll to that DIV and open it. Is this possible?

Thanks.

The url must be like: http://www.aaa.com/faq#1 (For 1st question) Hash shows that is 1st question. You can check is there any hash on URL below this code:

if(window.location.hash) {
  // Has exist
} else {
  // Hash doesn't exist
}

Then you should get hash from URL below this code:

var url = "http://www.aaa.com/faq#1";
var hash = url.substring(url.indexOf("#")+1);

This hash should be "1" in this example. And final step is scrolling to div:

window.location.hash = hash;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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