2013-09-24 36 views
1

我在頁腳中的jQuery手風琴菜單這是除了一個事實,即子菜單打開...頁面不瀏覽到錨鏈接偉大的工作。由於這是在我的頁腳,我不得不向下滾動查看打開的子菜單。我希望頁面自動向下滾動。jQuery的手風琴菜單不滾動到錨標記

爲什麼這是行不通的任何想法?我也試過把ID放在裏面,但那沒用。

我的HTML:

<ul class="footer-offices"> 
<li id="#sanfran" class="one"><a href="#sanfran">text</a> 
    <ul class="submenu"><li>office info here</li></ul> 
</li> 
</ul> 

jQuery是:

$(document).ready(function(){ 
$("ul.footer-offices li > a").on("click", function(e){ 
if($(this).parent().has("ul")) { 
    e.preventDefault(); 
} 
if(!$(this).hasClass("open")) { 
    // hide any open menus and remove all other classes 
    $("ul.footer-offices li ul").slideUp(350); 
    $("ul.footer-offices li a").removeClass("open"); 

    // open our new menu and add the open class 
    $(this).next("ul").slideDown(350); 
    $(this).addClass("open"); 
} 
else if($(this).hasClass("open")) { 
    $(this).removeClass("open"); 
    $(this).next("ul").slideUp(350); 
}  
}); }); 
+0

對於SO問題,這是太多的代碼。我建議你將它縮減到10行或更少的最相關的代碼。 –

+0

我刪除了CSS ...不知道是否有必要排除故障...我不知道我可以削減任何jQuery的東西。 – Beth

回答

0

這是這一部分:

<li id="#sanfran" class="one"> 

該ID應該只是說sanfran,沒有#<a href="#sanfran"中的#表示接下來是ID,所以#sanfran是多餘的。

這應該只是這樣的:

<li id="sanfran" class="one"> 

其他的一切看起來不錯。

0

這似乎正確的:

<ul class="footer-offices"> 
    <li id="sanfran" class="one"><a href="#sanfran">San Francisco Headquarters</a> 
    <ul class="submenu"><li>office info here</li></ul> 
    </li> 

    <li id="boston"><a href="#boston">Boston Headquarters</a> 
    <ul class="submenu"><li>office info here</li></ul> 
    </li> 
</ul> 

你有沒有在你的ID的#標籤不應該那裏?

你有這樣的:

<li id="#sanfran" class="one"> 

它應該是這樣的:

<li id="sanfran" class="one"> 

UPDATE:

是對的preventDefault在JavaScript防止跳?

if($(this).parent().has("ul")) { 
    e.preventDefault(); 
} 

如果刪除那段代碼會發生什麼?

+0

哎呀,對不起...我修好了,但頁面仍然沒有跳下去。 – Beth

+0

頁面是否存在?所以我們可以看到它? –

+0

您的javascript的preventDefault部分導致它不跳轉?往上看。 –