2013-03-07 51 views
0

我在頁面的右側有菜單,菜單中的所有鏈接都是橙色。當我將任何鏈接懸停時,它會變成黑色。但是,我想要的是,直到我點擊任何其他鏈接,它應該保持黑色活躍,以便每個人都知道打開的頁面屬於該鏈接。 這可能是一個愚蠢的問題,但我無法做到這一點。提前致謝。無法改變點擊鏈接的顏色

下面是代碼:

JavaScript函數:

@section JavaScript{ 
<script type="text/javascript"> 
    $('#allapps a').click(function() { 
     $('#allapps a').removeClass('selected'); // remove selected from any other item first 
     (this).addClass('selected'); //add selected to the one just clicked. 
    }); 
</script> 
} 

鏈接:

<a id="allapps" class="allapps" href="@Url.Action("CategoryType", "Marketplace", new { @id = 1 })"><h3 class="allapps grid_2 alpha">Legal </h3><p class="grid_1 omega calculate" > @ViewBag.legal</p><br /><br /></a> 

CSS:

.allapps 
{ 
font-family: Arial; 
font-size: 12px; 
color:#C55000; 
padding-left:20px; 
font-weight:bold; 
} 

a.allapps :link { 
    color: Black; 
} 

a.allapps :visited { 
color:Black;} 

a.allapps :hover { 
color:Black;} 

a.allapps :active { 
    color:Black; } 

回答

1

你錯過了$或jQuery的

變化

(this).addClass('selected'); 

$(this).addClass('selected'); 
+0

我試過了,但它仍然只要我上鍊接 – user207888 2013-03-07 17:26:46

0

在你的JQuery 爲什麼你使用ID名稱和標記名稱都....?

$('#allapps a').click(function() { 

你可以嘗試像下面...可能是它會幫助你..

$('#allapps').click(function() { 
     $('#allapps').removeClass('selected'); // remove selected from any other item first 
     $(this).addClass('selected'); //add selected to the one just clicked. 
    }); 

此外,我沒有發現你的CSS .selected類...

嘗試它添加

.selected{ 
color:Black;} 
+0

單擊我也試過,但仍然沒有工作 – user207888 2013-03-07 18:49:04

0

試試這個:

$(function(){ 
    var url = window.location.href; 
    var page = url.substr(url.lastIndexOf('/')+1); 
    $('a[href$="'+page+'"]').addClass('selected'); 

    $('#allapps a').click(function() { 
     $('#allapps a').removeClass('selected'); 
     $(this).addClass('selected'); 
    }); 
    }); 

什麼似乎想要to highlight the linkwhen clicked on it page get refreshedapplied class gets removed.

+0

是我的整個頁面得到刷新變爲橙色我嘗試以上但仍然沒有發生 – user207888 2013-03-07 18:48:46

+0

'var page = url.substr(url.lastIndexOf('/')+ 1);'這一行取決於你的url結構。如果你可以粘貼生成的鏈接html,那麼我可以很容易地幫助。 – Jai 2013-03-08 04:36:48