2013-10-28 55 views
0
function showSection(sectionID) { 
    $('div.section').css('display', 'none'); 
    $('div'+sectionID).css('display', 'block'); 
} 
$(document).ready(function(){ 
    if (
     $('ul#verticalNav li a').length && 
     $('div.section').length 
    ) { 
     $('div.section').css('display', 'none'); 
     $('ul#verticalNav li a').each(function() { 
      $(this).click(function() { 
       showSection($(this).attr('href')); 
      }); 
     }); 
     $('ul#verticalNav li:first-child a').click(); 
    } 
}); 
<ul id="verticalNav"> 
    <li><a href="#section1">Section I</a></li> 
    <li><a href="#section2">Section II</a></li> 
    <li><a href="#section3">Section III</a></li> 
</ul> 


    <div id="sections"> 
     <div class="section" id="section1"> 
      <p>Some content specific to this section...</p> 
     </div> 
     <div class="section" id="section2"> 
      <img src="#" alt="BADGER" /> 
     </div> 
     <div class="section" id="section3"> 
      <img src="#g" alt="SNAKE" /> 
     </div> 
    </div> 

我要創建特定鏈接的index.html#SECTION1每個標籤例如,index.html的#2節如何創建指向特定標籤的鏈接? (jQuery的)

+1

問題是什麼?什麼不行?它應該如何工作? – matewka

+0

在你提供的頁面中,一切正常。當你點擊鏈接時,它會隱藏所有部分,並顯示鏈接中指定的部分。然後,默認鏈接行爲導航到該部分的頂部。你是否想要__not__導航,但只顯示該部分? – matewka

+0

嗨matewka,我如何顯示默認部分。 – user2913707

回答

1

試試這個,

if(window.location.hash) 
{ 
    showSection(window.location.hash);// to get the div id 
} 

全碼

function showSection(sectionID) { 
    $('div.section').css('display', 'none'); 
    $('div'+sectionID).css('display', 'block'); 
} 
$(document).ready(function(){ 

    if (
     $('ul#verticalNav li a').length && 
     $('div.section').length 
    ) { 
     $('div.section').css('display', 'none'); 
     //$('ul#verticalNav li a').each(function() { // no need for each loop 
     $('ul#verticalNav li a').click(function() { // Use $('ul#verticalNav li a').click 
      showSection($(this).attr('href')); 
     }); 
     //}); 
     if(window.location.hash) // if hash found then load the tab from Hash id 
     { 
      showSection(window.location.hash);// to get the div id 
     } 
     else // if no hash found then default first tab is opened 
     { 
      $('ul#verticalNav li:first-child a').click(); 
     } 
    } 
}); 
+0

@ user2913707我沒有在您的頁面中找到'showSection function'。再次檢查我的「答案」。 –

+0

嗨Rohan Kumar,現在工作正常,非常感謝你 – user2913707

+0

頁面是垂直滾動時點擊標籤,如何修復。謝謝 – user2913707

0

回答您可以把每個部分的錨定標記與特定名稱。例如,對於SECTION1你會這麼做:

<a name="section1"> 
    <div class='section" id="section1"> 
     <p> Some content specific to this section...</p> 
    </div> 
</a> 

然後,你可以簡單地引用像這樣的部分(在同一頁面):

<a href="#section1">Click me to get to section1!</a> 

如果你嘗試和參考section1(這將讓我們從像about.html另一頁上說index.html),你只會有以下錨元素:

<a href="index.html#section1">Clicke me to navigate from about.html to section1 on index.html<a> 

此外,您可以嘗試使用window.location.hash [它所有主流瀏覽器都支持]如下:

window.location.hash = 'section1'; 
相關問題