2017-05-18 158 views
0

我有一個工作代碼,它爲標記爲active類的導航項添加句點,並在點擊導航到我導航中另一個頁面的鏈接時移動class="active"Javascript - 更改活動鏈接並將期間添加到鏈接的末尾

我需要添加幾行代碼,這些代碼會刪除當前鏈接末尾的句點並將其移至新點擊的活動鏈接。

有人可以幫助我這個代碼,因爲我有點失落?!

查看下面的代碼和圖像以供參考。

在此先感謝!

$(document).ready(function() { 
 
\t 'use strict'; 
 
\t // SITE MENU 
 
\t // This line of code adds the period to end of link 
 
\t document.getElementById("nav-item").innerHTML += "."; 
 
\t // These lines of code move the active class when a link is clicked 
 
\t $(function() { 
 
\t \t var current_page_URL = location.href; 
 
\t \t $("a").each(function() { 
 
\t \t \t if ($(this).attr("href") !== "#") { 
 
\t \t \t \t var target_URL = $(this).prop("href"); 
 
\t \t \t \t if (target_URL == current_page_URL) { 
 
\t \t \t \t \t $('.nav a').parents('li, ul').removeClass('active'); 
 
\t \t \t \t \t $(this).parent('li').addClass('active'); 
 
\t \t \t \t \t return false; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
});

enter image description here

回答

0

你可以用CSS來完成這個嗎?由於不包含HTML,因此我不確定代碼的結構是什麼,但假設您可以定位活動鏈接,則可以使用類似於:

.active:after { 
    content: "."; 
} 
0

而是與innerHTML的添加時間段的,你可以使用CSS來插入一段如下圖所示:

.active:after { 
    content: "."; 
} 

所以每當活躍類被添加到一個元素,它會添加句點到結尾o內容。