本文介绍了jQuery从链接打开手风琴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做什么错.手风琴可以工作,但是当我尝试从外部链接(fx.mysite.com/mypage.php#2)打开它时-它不会打开手风琴!
What am I doing wrong.The accordion works, but when I try to open it from external link (fx. mysite.com/mypage.php#2) - it wont open the accordion!
我的标题是:
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#accordion").accordion({
active: false,
collapsible: true,
autoHeight: false,
navigation: true,
header: '.menuitem'
});
$(".menuitem").click(function(event){
window.location.hash=this.hash;
});
});
</script>
我的html是:
<div id="accordion">
<a class="menuitem" href="#1">Header 1</a>
<!-- accordion panel --><div>
CONTENT 1</>
<!-- end accordion panel --></div>
<a class="menuitem" href="#2">Header 2</a>
<!-- accordion panel --><div>
CONTENT 2</>
<!-- end accordion panel --></div>
<!-- end accordion -->
推荐答案
以下是工作代码- Jsfiddle链接
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#accordion").accordion({
active: false,
collapsible: true,
autoHeight: false,
navigation: true,
header: 'a.menuitem'
});
$(".menuitem").click(function(event){
window.location.hash=this.hash;
});
//get the hash value
var locationHash = window.location.hash;
//split the value
var hashSplit = locationHash.split('#');
//get the tab number
var currentTab = hashSplit[1];
window.setTimeout(function(){
//open the tab for the current hash
$("#accordion").accordion({ active: parseInt(currentTab)-1 });
}, 1000);
});
</script>
</head>
<body>
<div id="accordion">
<a class="menuitem" href="#1">Header 1</a>
<!-- accordion panel -->
<div>
CONTENT 1
</div>
<a class="menuitem" href="#2">Header 2</a>
<div>
CONTENT 2
</div>
<!-- end accordion -->
</div>
</body>
</html>
这篇关于jQuery从链接打开手风琴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!