2017-02-09 83 views
1

我有下面的代碼獲取標記

  $('li').click(function(e){ 
     e.preventDefault(); 
     $(this).addClass("active1"); 

    // $(this a).addClass("acolor"); // I am trying this 

});; 

錨標籤是L1標籤內,對李的點擊我要改變文本的顏色 - 這是有在CSS中.acolor類

在這我得到李,我想要一個標籤。

任何幫助將不勝感激!謝謝

+0

我發現重複通過搜索['[jquery的]發現裏面this'元件](http://stackoverflow.com/search?q=%5Bjquery%5D+find+element+inside+this) ,這讓我去http://stackoverflow.com/questions/10604251/call-elements-inside-this。 –

回答

1

使用find()方法。

$(this).find('a').addClass("acolor"); 

,或者提供this如jQuery的方法的context

$('a', this).addClass("acolor"); 
+0

非常感謝你Pranav!它爲我工作:) – CodeWithCoffee

+0

將此作爲活動添加後,我怎麼能刪除它,一旦我點擊另一個李並從目前李刪除? – CodeWithCoffee

+0

@CodeWithCoffee'$(this).sibling()。find('a')。removeClass(「acolor」); –