简体   繁体   中英

Using this with pseudo selectors jquery

I'd like to know if there is a way to use this with CSS pseudo selectors - (AKA nested elements) in jQuery. Here's what it might look like.

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

But this isn't how the syntax works. I'd just like to know how it would work.

when you use $(this) use .find() to get the element you want like this

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

Use find() instead. Example:

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

You can either use .css() or also toggle a class in jQuery and set the CSS property inside your CSS file which It's usually more performing and reusable.

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

or also

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

CSS

.height-nested{
   height: 100px;
}

Use following code

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

Or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM