简体   繁体   English

与伪选择器jquery一起使用

[英]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. 我想知道是否有使用方式this与CSS伪选择- (AKA嵌套元素)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 当您使用$(this)时,请使用.find()来获取所需的元素,如下所示

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

Use find() instead. 使用find()代替。 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. 您可以使用.css(),也可以在jQuery中切换一个类,并在CSS文件中设置CSS属性,这通常更具性能和可重用性。

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

or also 或者也

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

CSS 的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');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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