2014-02-21 163 views
0

我做錯了什麼。 手風琴的作品,但當我試圖從外部鏈接(fx。mysite.com/mypage.php#2)打開它 - 它不會打開手風琴!jquery打開手風琴從鏈接

我的頭是:

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

外部鏈接意味着什麼? – kbvishnu

回答

2

這裏是工作的代碼 - 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

你的手風琴是在你的網頁動態元素(與一些JavaScript代碼驅動)。

在您的url中設置一個html錨點(如#2)不會觸發accordeon代碼,您只是指示瀏覽器導航到錨定元素!

您需要向頁面添加一些JavaScript代碼才能獲取錨點的值並將此參數傳遞給您的accordeon小部件。

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

許多變化,可能需要..