Accordion JS不会从标签链接打开
Accordion JS wont open from a hashtag link
我有一个手风琴短代码工作在我的页面很好,但当我有一个链接的内容,我想在手风琴打开一个特定的面板它将不起作用。也许有人可以引导我在路径编辑我的JS代码?
/**
* Main JavaScript
*/
// Document is loaded...
jQuery(document).ready(function($) {
/******************************************
* ACCORDION (Shortcode)
**************************************/
var accordion_active_class = 'accordion-active';
// Loop each instance
$('.accordion').each(function() {
var accordion_wrapper = this;
var sections = $('> section', accordion_wrapper);
// Make sure only one active section on load
var active_section_set = false;
$(sections).each(function(index) {
// Section is active
if ($(this).hasClass('accordion-active')) {
// Another was already set
if (active_section_set) {
$(this).removeClass('accordion-active'); // hide section
}
// Allow only one active section
active_section_set = true;
}
});
// Click on section
$('.accordion-section-title', sections).click(function() {
var section = $(this).parent();
// if clicked section was not self
if (!$(section).hasClass(accordion_active_class)) {
// hide all section content
$('.accordion-content', sections).hide();
// show current section content
$('.accordion-content', section).hide().fadeTo(500, 1); // fadeTo looks better than fadeIn in IE7
// move active class to new active section
sections.removeClass(accordion_active_class);
$(section).addClass(accordion_active_class);
}
// if it was self, close it
else {
$('.accordion-content', sections).hide();
sections.removeClass(accordion_active_class);
}
});
});
// CSS fixes for IE8 which doesn't support :not or :last-child
$('.accordion section .accordion-content > :last-child').css('margin-bottom', '0');
$('.accordion section:not(.' + accordion_active_class + ') .accordion-content').hide();
// Mysterious IE8 layout bug fix
// http://stackoverflow.com/questions/3350441/dynamic-elements-are-not-appearing-in-ie8-until-there-is-a-mouse-click
$('.accordion section').addClass('dummyclass').removeClass('dummyclass');
});
这是输出的HTML:
<div class="accordion">
<section id="title1" class="">
<div class="accordion-section-title">Title 1</div>
<div class="accordion-content" style="display: none; opacity: 1;">
<p>This is some text.</p>
</div>
</section>
<section id="title2" class="">
<div class="accordion-section-title">Title 2</div>
<div class="accordion-content" style="display: none; opacity: 1;">
<p>This is some more text.</p>
</div>
</section>
</div>
最后这是我想要触发下拉菜单的链接的结构:
<a href="#title1">Title 1 - trigger</a>
同一页面上的散列链接不会在设计良好的浏览器上重新加载页面,并且通常不会触发任何事件(您可以使用js调试器检查这一点)。
我认为你需要钩上window.onhashchanged
。但是这个方法不支持某些浏览器(safari和IE<8 IFRC),所以你可能对jquery哈希改变插件感兴趣
我让它工作,我改变了JS如下:
jQuery(document).ready(function($) {
// ACCORDION
var accordion_active_class = 'accordion-active';
var sections = $('.accordion > section');
var section_headings = $('.accordion > section .accordion-section-title');
function scrollToSection(section) {
$('html, body').animate({
scrollTop: parseInt($(section).offset().top) - 10
});
}
function openSection(section) {
// if not already open
if (!$(section).hasClass(accordion_active_class)) {
// hide all section content
$('.section-content', sections).hide();
// show current section content
$('.section-content', section).hide().fadeTo(500, 1); // fadeTo looks better than fadeIn in IE7
// move active class to new active section
sections.removeClass(accordion_active_class);
$(section).addClass(accordion_active_class);
// scroll there, because if a really big section was closed, things are still off
scrollToSection(section);
}
}
section_headings.click(function() {
var section = $(this).parent();
// if clicked section is not active
if (!$(section).hasClass(accordion_active_class)) {
openSection(section);
}
// clicked section is active, collapse it
else {
// hide section content
$('.section-content', sections).hide();
// remove active class
sections.removeClass(accordion_active_class);
}
});
// CSS fixes for IE7/8 which doesn't support :not or :last-child
$('.accordion section .section-content > p:last-child').css('margin-bottom', '0');
$('.accordion section:not(.' + accordion_active_class + ') .section-content').hide();
/* Scroll to and open section */
$("a[data-rel^='openSection']").click(function(event) {
// stop click action
event.preventDefault();
/* which section? */
var section = $($(this).attr('href'));
/* open section */
openSection(section);
/* scroll to it */
scrollToSection(section);
});
// Scroll to section via hash tag
if(window.location.hash) {
openSection(window.location.hash);
}
});
然后我添加了如下链接:
<a data-rel="openSection" href="#frequently-asked-questions">Frequently Asked Questions</a>
相关文章:
- 按钮标签,javascript 转换为链接标签
- 需要查找和替换链接标签
- 如何使html链接标签可以在没有标签的情况下工作
- 如何显示下拉 Angularjs-ui-select onclick on 超链接标签
- 如何将图像标签插入超链接标签 在 动态使用 jQuery 中
- 如何通过双击而不是单击触发链接(标签)
- 在使用 jQuery 时单击链接标签
- 如何在 css 链接标签中插入 javascript 函数
- 如何为脸书image_src添加链接标签
- 脚本/链接标签与 AJAX 加载
- 如何使用HTML或Javascript将用户输入的网站识别为链接标签
- 当我们点击链接标签javascript中的跨度时,要防止链接的行为
- 链接标签dosen'我不能在Emberjs中工作
- 在html链接标签中打开所有url,而不使用javascript转换任何其他链接
- 如何添加数据切换到链接标签,如果在li中有ul
- JointJS:使链接标签在链接本身上移动
- Jquery循环页面缩略图图像-保留主图像链接标签
- 从网页中删除CSS(动态加载,编译,不链接标签)
- 如何增加链接或链接标签之间的距离
- 尝试创建一个超链接标签在文本框中输入文本时