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

Can I use :selectorwith the following in jQuery?

$('.galler_attr').bind('click', function() {
   $(this:first-child).css('color', 'red'); 
      });
+3
source share
3 answers

No. You are trying to mix a function context with a string.

Use thisas a selector context or call on to search within the element's DOM area. Or find()$(this)

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

or this (which eliminates this internally):

$('.galler_attr').bind('click', function() {
   $(':first-child', this).css('color', 'red'); 
});
+14
source

Yes, you can use it, but not that way. You can say that, as Russ says.

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

You can get the best help for such problems by going to www.visualjquery.com. That is pretty.

+1

.is()

$('.galler_attr').bind('click', function() {
   $(this).is(':first-child').css('color', 'red');
   // this would change the color to red if the clicked element is a first-child....
});

, ?

$('.galler_attr:first-child').bind('click', function() {
   // this would bind click event to the first-child element...
   $(this).css('color', 'red');

});
0

Source: https://habr.com/ru/post/1756316/


All Articles