jQuery手风琴,打开一个基于href的框 [英] jquery accordion, opening a box based on href
问题描述
我正在尝试根据发送到页面的链接打开手风琴
I am trying to open an accordion based on a link i send to the page
这是我的网址
services.html#branding
services.html#branding
我在脑海中使用以下代码:
I am using the following code in the head:
<script type="text/javascript">
$(document).ready(function(){
$('#accordion').accordion({collapsible: true, animated: 'slide', autoHeight: false, navigation: true, active : 'false'});
});
</script>
手风琴看起来像:
<div id="accordion">
<h3 id="branding"><a href="#">Branding</a></h3>
<div>
<p>Does your business have a</p>
</div>
<h3><a href="#print">Print</a></h3>
<div>
<p>Brochures</a></p>
</div>
</div>
任何帮助将不胜感激... http://docs.jquery.com/UI/Accordion
Any help would be greatly appreciated... http://docs.jquery.com/UI/Accordion
推荐答案
哦,我看到Jeff报告说当前的UI版本已损坏,但实际上我有使用当前版本的解决方案...
Oh I see Jeff reported that the current UI version is broken, but I actually had a solution using the current version...
HTML
<div id="accordion">
<h3><a href="#branding">Branding</a></h3>
<div>
<p>Does your business have a</p>
</div>
<h3><a href="#print">Print</a></h3>
<div>
<p>Brochures</p>
</div>
</div>
脚本
$(function(){
$('#accordion').accordion({
collapsible: true,
animated: 'slide',
autoHeight: false,
navigation: true
});
// open content that matches the hash
var hash = window.location.hash;
var thash = hash.substring(hash.lastIndexOf('#'), hash.length);
$('#accordion').find('a[href*='+ thash + ']').closest('h3').trigger('click');
})
我最初使用的是a[href$=...]
,但是将其更改为a[href*=...]
...都可以使用
I used a[href$=...]
originally, but changed it to a[href*=...]
... either will work
更新: navigation
选项已从jQuery UI 1.10.0中删除,请使用此方法:
Update: the navigation
option was removed from jQuery UI 1.10.0, so use this method:
CSS
.accordion {
position: relative;
}
.accordion .accordion-link {
position: absolute;
right: 1%;
margin-top: 16px;
z-index: 1;
width: 12px;
height: 12px;
background: url(link.png) center center no-repeat; /* approx 12x12 link icon */
}
脚本
var hashId = 0,
$accordion = $('#accordion');
if (window.location.hash) {
$accordion.children('h3').each(function(i){
var txt = $(this).text().toLowerCase().replace(/\s+/g,'_');
this.id = txt;
if (txt === window.location.hash.slice(1)) {
hashId = i;
}
});
}
$accordion.accordion({
active: hashId,
animate: false,
heightStyle: 'content',
collapsible: true,
create: function( event, ui ) {
$accordion.children('h3').each(function(i){
$(this).before('<a class="accordion-link link" data-index="' + i + '" href="#' + this.id + '"></a>');
});
$accordion.find('.accordion-link').click(function(){
$accordion.accordion( "option", "active", $(this).data('index') );
});
}
});
这篇关于jQuery手风琴,打开一个基于href的框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!