2016-05-17 27 views
0

我有一個'a'標籤,點擊它後我正在重定向到另一個頁面。但是我想從該頁面選擇一個元素並將類添加到它。JS重定向到另一個頁面並將類追加到其中一個元素

HTML:

<a href="another pages url">blah</a> 

的JavaScript

$('a').click(function(){ 
    $("#elementsId").addClass("selected"); 
}); 
+0

是#elementsId在當前頁面或目標頁面? –

+0

目標頁面。也許我應該在我的jQuery代碼中加載那些頁面數據? – Artdark92

回答

0

當你點擊一個URL鏈接時,頁面被刷新和當前的Jquery不會再工作。您必須更新您的代碼,以根據頁面加載的新目標頁面URL或DOM ready而不是onClick確定活動的<a>鏈接。

您必須將選定的類設置爲具有找到的文件名作爲href屬性的菜單。

找到當前網址和集選擇類相關鏈接:

$(document).ready(function(){ 
    //Determine current page url 
    var fullpath =window.location.pathname; 
    var filename=fullpath.replace(/^.*[\\\/]/, ''); 
    //alert(filename); 

    //Selects the proper a tag 
    var currentLink=$('a[href="' + filename + '"]'); 
    currentLink.addClass("selected"); 
    })) 
相關問題