2014-12-06 111 views
0

我想知道是否有一种方法可以使用jQuery中的CSS伪选择器 - (AKA嵌套元素)this。这可能是什么样子。使用伪选择器jquery

$('#element').click(function(){ 
     $(this' .nested').css('height','100px'); 
    }); 

但是,这不是语法的工作原理。我只想知道它是如何工作的。

回答

0

使用find()代替。例如:

$('#element').click(function(){ 
    $(this).find('.nested').css('height','100px'); 
}); 
+0

非常感谢。太棒了。 – 2014-12-06 05:38:21

+0

你是最受欢迎的:) – MH2K9 2014-12-06 05:40:56

0

当您使用$(本)使用.find()来获取元素,你想 这样

$('#element').click(function(){  
    $(this).find('.nested').css('height','100px'); 
}); 
0

您可以使用的CSS(),或者也切换jQuery的一类,并设置你的CSS文件中的CSS属性,它它通常更多的表演和可重复使用。

$('#element').click(function(){  
    $(this).find('.nested').css('height','100px'); 
}); 

或也

$('#element').click(function(){  
    $(this).find('.nested').toggleClass('height-nested'); 
}); 

CSS

.height-nested{ 
    height: 100px; 
} 
0

使用下面的代码

$('#element').click(function(){ 
    $('.nested',this).css('height','100px'); 
}); 

或者

$('#element').click(function(){ 
    $(this).find('.nested').css('height','100px'); 
}); 
+0

我喜欢你的答案,只是为了增加性能的东西,首先是对第二个慢的尊重http://stackoverflow.com/a/8083631/1018054 – 2014-12-06 05:59:11