jquery 从链接打开手风琴 [英] jquery open accordion from link

查看:38
本文介绍了jquery 从链接打开手风琴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了什么.手风琴可以工作,但是当我尝试从外部链接 (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><脚本>$(document).ready(function() {$("#accordion").手风琴({活动:假,可折叠:真实,自动高度:假,导航:真的,标题:'.menuitem'});$(".menuitem").click(function(event){window.location.hash=this.hash;});});

我的 html 是:

 

<a class="menuitem" href="#1">标题 1</a><!-- 手风琴面板--><div>内容 1 </><!-- 结束手风琴面板--></div><a class="menuitem" href="#2">标题 2</a><!-- 手风琴面板--><div>内容 2<!-- 结束手风琴面板--></div><!-- 结束手风琴 -->

解决方案

这是工作代码 - 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"><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>无标题文档</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><脚本>$(document).ready(function() {$("#accordion").手风琴({活动:假,可折叠:真实,自动高度:假,导航:真的,标题:'a.menuitem'});$(".menuitem").click(function(event){window.location.hash=this.hash;});//获取哈希值var locationHash = window.location.hash;//分割值var hashSplit = locationHash.split('#');//获取标签号var currentTab = hashSplit[1];window.setTimeout(function(){//打开当前哈希的选项卡$("#accordion").accordion({ active: parseInt(currentTab)-1 });}, 1000);});<身体><div id="手风琴"><a class="menuitem" href="#1">标题 1</a><!-- 手风琴面板--><div>内容 1

<a class="menuitem" href="#2">标题 2</a><div>内容 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!

My header is:

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

My html is:

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

解决方案

Here is the working code - 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 从链接打开手风琴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆