2013-12-16 37 views
0

我正在使用jquery將該類添加到「li」上的活動css,並導航到html頁面,但在頁面導航「li」上的類disappers之後。我嘗試過不同的方法來解決這個問題,但沒有明白。在li上添加class在頁面加載後刪除

$(document).ready(function(){ 
    $('#topList ul li').click(function() { 
     alert($(this).attr('id')); 


     if($(this).attr('id') == "add") {    
      document.location.href = 'localhost:8080/add'; 
      $(this).attr('id').addClass("active");    
     } 
    });  
}); 

這裏是菜單列表,我想要的是當我點擊李應調用頁面中添加,也對李加個班。

HTML代碼

<ul class="nav"> 
    <li class="" id="add"><a href="javascript:void(0);" ><i class="add"></i> Add</a>  </li> 
<ul> 
+0

對不起,我編輯了這個問題。 Pleae現在檢查 –

+0

你說'懸停'狀態,但你正在使用'點擊'事件! – DontVoteMeDown

+0

無論如何,我相信你應該使用這個:'$(this).addClass(「active」)'。 – DontVoteMeDown

回答

1

在菜單或鏈接所在的頁面上使用以下腳本。

<div id="cssmenu"> 
<a href="blah blah" > Test page</a> 
<a href="blah blah" > Test page</a> 
</div> 





    $(document).ready(function() { 
       var pageTitle = window.location.pathname.replace(/^.*\/([^/]*)/, "$1"); 

       $('#cssmenu a').each(function() { 
        if ($(this).attr('href').toLowerCase() == pageTitle.toLocaleLowerCase()) 
         $(this).addClass('active'); 
       }); 



      }); 
+0

好的。那麼如何在同一時間導航一個頁面? –

+0

您可以使用與您已經做過的相同的代碼進行重定向。這僅用於控制活動頁面上的活動類。看到更新的答案。 –

+0

我用你唯一的window.location.pathname.replace代碼行,它工作,所以沒有使用下面的代碼。 :)。謝謝。我在我的if條件中使用了這個代碼,並且hrefs已經在html中進行了修改。休息的if將與你的這一行代碼window.location.pathname一起工作。再次感謝。 –

1

我通過查看URL,並決定該導航元素是最好的補充解決在my website這個問題。

function showContent(target) { 
    var e = $('#'+target); 
    $(".content").hide(); 
    $("#nav li.active").removeClass("active"); 
    $("#nav li[target='"+target+"']").addClass("active"); 
    e.toggle(); 
    ga('send','pageview',window.location.pathname+"#"+target); 
} 

// this looks at the URL (by the #...) and decides which view is active 
// this gets called on ready and if the client hits the link 
// (which dynamically loads instead of making a trip for a new page to the server) 
$(window).hashchange(function() { 
    var which=window.location.hash.substring(1); 
    switch(which) { 
    default: 
     which="profile"; 
    case "profile": 
    case "resume": 
    case "projects": 
    case "contact": 
     showContent(which); 
    } 
}); 
2

您需要添加類爲你調用即頁面裏,當你調用localhost上的頁面將呈現:8080 /加。 因爲在代碼中設置活動類不會被調用的,因爲本地主機:8080 /加將在代碼(document.location.href = 'localhost:8080/add';)

前行開始加載如果要呈現的頁面是靜態頁面,然後添加以下代碼在該頁面中。

$(document).ready(function(){ 
$('#add').addClass("active"); 
}); 
+0

好的,但是如何解決呢?你能給他一些解釋嗎? –

+0

我知道這是事實,但如何解決這個問題?調用url並在li上添加一個類。 –

+0

你正在渲染的是一個靜態的html頁面? –