2017-02-09 84 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”); –