jquery 从链接打开 Accordion

标签 jquery accordion

我做错了什么。 Accordion 可以工作,但是当我尝试从外部链接(fx.mysite.com/mypage.php#2)打开它时 - 它不会打开 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 Link

<!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 从链接打开 Accordion ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932607/

相关文章:

javascript - 如何检查 jquery 或 javascript 中的时间重叠

jquery - 使用 jQuery 隐藏父 div 问题

jquery - 更改 Jquery UI Accordion 的动画速度

javascript - Twitter Bootstrap 崩溃了,但首先

jquery - .addClass 与 JQuery 的特异性?

jquery - 如何在 Jquery 中保留选项卡的状态?

javascript - 如何在 UIWebView 中调整图像大小?

javascript - Accordion 中的下拉菜单,想要在不更改 Accordion 面板的情况下进行选择

javascript - CKEditor 改变了我所有的 HTML

javascript - Accordion 标题上的按钮重定向 angularjs