来自链接的jquery open accordion

时间:2014-02-21 11:02:52

标签: jquery accordion

我做错了什么。 手风琴有效,但是当我尝试从外部链接打开它时(fx.mysite.com/mypage.php#2) - 它不会打开手风琴!

我的标题是:

<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 -->

5 个答案:

答案 0 :(得分:3)

以下是工作代码 - 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>

答案 1 :(得分:1)

@Ashis 你的代码只能完美运行,每个DIV都有相同的大小,因此,当我有一条带有2行的DIV时,下面会有很多空白区域。当它包含大量信息时,DIV很好地填充。

答案 2 :(得分:0)

if ($.url().hash.length) {
    var item = $.url().hash;    
    $('#accordion').accordion({active: item - 1}); // since you started numbering at 1 and not 0.
}

答案 3 :(得分:0)

你可以做这样的事情,虽然不是一个完美的解决方案:

$(document).ready(function() {
    var $hash = window.location.hash;
    var $acc = $hash ? $hash : 1;
    $("#accordion").accordion({
     active: false,
     collapsible: true,
     autoHeight: false,
     navigation: true,
     header: '.menuitem',
     activate: $acc
   });
});  

可能需要进行许多更改..

答案 4 :(得分:-1)

您的accordeon是您网页中的动态元素(使用一些javascript代码驱动)。

在你的网址中设置一个html锚点(比如#2)不会触发accordeon代码,你只是指示浏览器导航到锚定元素!

您需要在页面中添加一些javascript代码以获取锚点的值,并将此参数传递给您的accordeon小部件。

相关问题