1

我正在为我的公司创建一个常见问题解答/帮助中心页面。我们试图完成的最后一件事是“热门问题部分”,用户只需点击问题,它就会打开指向问题所在页面的链接,并且手风琴打开正确的部分以显示答案.

$(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();
});


});

这是用于手风琴的 jQuery,完整的工作代码在这里http://jsfiddle.net/gvolkerding/ancu6fgu/3/ 一个例子是,如果我们提出了最重要的问题之一“我如何注册接收促销电子邮件?”,则需要在打开手风琴第 4 部分的情况下加载页面。我们有 8 个单独的页面,上面有问题,所以理想情况下,我所要做的就是在一个链接后面加上一个查询(或您能想到的任何其他方式)以指向正确的页面/问题。我非常感谢提供的任何帮助,谢谢大家。

4

2 回答 2

1

在小提琴中,您使用 ID(例如手风琴-3)来识别、显示和隐藏手风琴部分。您还可以将该 ID 用作指向您的常见问题页面的任何链接的锚点。将以下代码放在函数末尾document.ready

// 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');
}  

并且到页面的链接会有点像/path/to/faq.html#accordion-3。手风琴3是您要打开的锚点/元素 ID。请注意,页面也会滚动到具有相应 ID ( accordion-3 ) 的元素的位置。为避免这种情况,您必须在触发点击后滚动到顶部,或者您将使用 GET 参数而不是位置哈希。

更新:包括滚动到问题的页面

由于下面的评论,这里的版本包括滚动到活动问题的解决方案。

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
        });
    }       

}  

更新的小提琴在这里

于 2015-06-08T15:46:50.193 回答
0

您可以这样做的一种方法是将问题索引作为查询参数传递。然后,您将在代码中获取参数值,qIndex然后添加以下内容:

//get question index passed via query parameter
var qIndex = .....

$('.accordion-section-title').click(function(e) {
    ...
})
.eq( qIndex ).trigger('click'); //<<<------------

演示

于 2015-06-08T15:30:08.800 回答