问题
I am working on a FAQ/Helpcenter page for my company. One of the last things we are trying to accomplish is a "top questions section" where users can just click on the question and it opens a link to the page the question is on and the accordion is open to the correct section to display the answer.
$(document).ready(function() {
function close_accordion_section() {
$('.accordion .accordion-section-title').removeClass('active')
.find('img').attr('src', 'http://www.scrubsandbeyond.com/app_themes/scrubsandbeyond/graphics/right.png');
$('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}
$('.accordion-section-title').click(function(e) {
// Grab current anchor value
var currentAttrValue = jQuery(this).attr('href');
if($(this).is('.active')) {
close_accordion_section();
}else {
close_accordion_section();
$(this).find('img').attr('src', 'http://www.scrubsandbeyond.com/app_themes/scrubsandbeyond/graphics/down.png');
// Add active class to section title
$(this).addClass('active');
// Open up the hidden content panel
$('.accordion ' + currentAttrValue).slideDown(300).addClass('open');
}
e.preventDefault();
});
});
This is the jQuery used for the accordion, and the full working code is here http://jsfiddle.net/gvolkerding/ancu6fgu/3/ One example would be, if we made one of the top questions "How do I sign up to receive promotional emails?", then the page would need to load with accordion section 4 open. We have 8 separate pages with questions on them, so ideally all I would have to do is put in a link with a query after it(or any other way that you could think of) to point to the correct page/question. I really appreciate any help that is offered, thanks everyone.
回答1:
In the fiddle you use ID's (for example accordion-3) to identify, display and hide the accordion sections. That ID you can also use as an anchor for any link to your faq-pages. Put the following code at the end of the document.ready
function:
// current location has anchor
if(location.hash) {
// find accordion href ending with that anchor
// and trigger a click
$(".accordion-section-title[href$="+location.hash+"]").trigger('click');
}
and the link to the page would be somethink like /path/to/faq.html#accordion-3
. Where accordion-3 is the anchor / element-ID you'll want to open. Be aware that the page also scrolls to the position of the element with the corresponding ID (accordion-3). To avoid this, you'll either have to scroll to top after trigger the click, or you'll use a GET-parameter instead of location hash.
Update: including a page scrolling to the question
Due the comment below, here a version including a solution to scroll to the active question.
if(location.hash) {
// the selector
var currentTarget = $(".accordion-section-title[href$="+location.hash+"]"),
offset = {top: 0, left: 0};
// in case we have a hit...
if(currentTarget.length) {
// trigger the click
currentTarget.trigger('click');
// get current offset (relative to document, contains left and top)
// since the selector is the question (not the answer with the id)
// this will show the question right on top
offset = currentTarget.offset();
// just in case you'll want to add some extra space do it like this:
// offset.top -= 10;
// and let the page scroll to this position
$('html, body').animate({
scrollTop: offset.top,
scrollLeft: offset.left
});
}
}
The updated fiddle is here.
回答2:
One way you can do that is to pass the question index as a query parameter. Then you would get the parameter value in your code, say, qIndex
and then add the following:
//get question index passed via query parameter
var qIndex = .....
$('.accordion-section-title').click(function(e) {
...
})
.eq( qIndex ).trigger('click'); //<<<------------
DEMO
来源:https://stackoverflow.com/questions/30712958/how-to-load-page-with-accordion-open-based-on-external-link