2010-07-08 14 views
0

我有兩個單獨的jQuery函數,允許用戶切換div的可見性。按照規定,分區將處於關閉狀態。我試圖找出一種方法來設置鏈接到這個頁面,將打開一個特定的div基於傳遞給查詢字符串的ID。我的jQuery看起來是這樣的:jQuery通過查詢字符串觸發事件

jQuery(document).ready(function(){ 

    //make first div always open 

    jQuery(".toggle_container:not(:first)").hide(); 
    jQuery(".toggle_container:first").show(); 
    jQuery("h6.trigger:first").addClass("active"); 

    jQuery("h6.trigger").click(function(){ 
     jQuery(this).toggleClass("active"); 
     jQuery(this).next(".toggle_container").slideToggle(300); 
    }); 

    //The second function 

    jQuery(function(){ 
     jQuery(".toggleIt").click(function(){ 
      jQuery(this).next(".hiddenText").slideToggle("fast"); 
      jQuery(this).html(function(i,html) { 
       if (html.indexOf('+') != -1){ 
        html = html.replace('+','-'); 
       } else { 
        html = html.replace('-','+'); 
       } 
       return html; 
      }) 
     }); 
    }); 

HTML

<h6 class="trigger" id='uniqueID"></h6> 
<div class="toggle_container"> 
TEST 
</div> 

//and the second toggled container 
<p class="toggleThis tips" id="AnotherID">+</p> 
<div class="hiddenText2"> 
Another TEST 
</div> 

我想一個ID添加到URL的查詢字符串,並把它打開隱藏的div用相同的ID。到目前爲止,我有這樣的結構...

var category = getParameterByName('cat'); 
var id = getParameterByName('id'); 

if(id) 
{ 

} 
else if(category) 
{ 

} 
else 
{ 
    //moving this in here 
    jQuery(".toggle_container:not(:first)").hide(); 
    jQuery(".toggle_container:first").show(); 
    jQuery("h6.trigger:first").addClass("active"); 
} 

我陷在這部分。有小費嗎?

回答

0

您是否使用PHP或其他語言來創建HTML?..我會創建一個隱藏字段,填充查詢變量,並使用onLoad打開div,如果該字段已填充。

+0

它確實需要通過查詢字符串傳遞。 – Peachy 2010-07-08 17:20:38

+0

是的,我同意......你通過查詢字符串傳遞它。您的頁面創建語言將其嵌入到HTML輸入元素中作爲值。 jQuery選擇並採取行動。 – Fosco 2010-07-08 17:33:24

+0

也許我誤解了。儘管如此,我確實需要保持這一點。我相當確定有一種方法可以觸發點擊事件,或者使用hash + var觸發此事件。只是不能把它釘死。雖然謝謝! – Peachy 2010-07-08 18:03:56