jQuery this和Selector $(this:first-child).css('color','red');

hoe*_*erf 4 jquery this selector

是否可以:selector在jQuery中使用以下内容?

$('.galler_attr').bind('click', function() {
   $(this:first-child).css('color', 'red'); 
      });
Run Code Online (Sandbox Code Playgroud)

Rus*_*Cam 16

不.您正在尝试将函数上下文与字符串混合使用.

使用this情况下选择的,或致电find()$(this)的元素的DOM范围内进行搜索.或

$('.galler_attr').bind('click', function() {
   $(this).find(':first-child').css('color', 'red'); 
});
Run Code Online (Sandbox Code Playgroud)

或者这个(内部解决了上述内容):

$('.galler_attr').bind('click', function() {
   $(':first-child', this).css('color', 'red'); 
});
Run Code Online (Sandbox Code Playgroud)