jquery 这个子选择器?

发布于 2024-10-08 14:13:06 字数 390 浏览 0 评论 0原文

快速提问,我在网上找不到任何内容。我里面有一个父 div 和一个子 div。要使用 css 选择它,您会说:

#parent .child {}

jQuery 中,我的父元素有一个变量。但是,我该如何选择这个孩子呢?我知道创建新变量很容易,我只是好奇这是否可能?

const parent = $('#parent');
    
parent.click(function() {
  $(this > '.child').hide();
})

谢谢

Quick question, I couldn't find anything on the web. I have a parent div and a child div inside of it. to select it with css you would say:

#parent .child {}

In jQuery, I have a variable for my parent element. However, how can I select the child with this? I know it's easy to create a new variable, I'm just curious if it's possible?

const parent = $('#parent');
    
parent.click(function() {
  $(this > '.child').hide();
})

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏花。依旧 2024-10-15 14:13:10

你可以找到父元素的子元素并应用CSS。

示例代码如下。

var Parent = $('#parent');

Parent.click(function() {
$(this).find('.child').hide();});

U can find children of your parent elements and apply css.

Sample code is below.

var Parent = $('#parent');

Parent.click(function() {
$(this).find('.child').hide();});
撑一把青伞 2024-10-15 14:13:09

试试这个代码:

  $("#parent").click(function () {
               $(this).next().hide();
});

try this code:

  $("#parent").click(function () {
               $(this).next().hide();
});
把昨日还给我 2024-10-15 14:13:09
var Parent = $('#parent');

Parent.click(function () {
    $(".child", this).hide();
});

或者

$("#parent").click(function () {
    $(".child", this).hide();
});
var Parent = $('#parent');

Parent.click(function () {
    $(".child", this).hide();
});

or

$("#parent").click(function () {
    $(".child", this).hide();
});
弱骨蛰伏 2024-10-15 14:13:08

您可以只调用 .find() 方法:

var Parent = $('#parent');

Parent.click(function() {
    $(this).find('.child').hide();
});

如果您只想选择直接子级,请使用 .children() 方法:

Parent.click(function() {
    $(this).children('.child').hide();
});

人们经常使用如下语法

$('.child', this);

:还有。这对我来说不太方便,因为你以某种方式写了一个“反向”订单。不管怎样,这个语法在内部被转换成 .find() 语句,所以你实际上是在保存一个调用。

参考:.find().children()

You may just invoke the .find() method:

var Parent = $('#parent');

Parent.click(function() {
    $(this).find('.child').hide();
});

If you only want to select the immediate children, use the .children() method instead:

Parent.click(function() {
    $(this).children('.child').hide();
});

People often use a syntax like

$('.child', this);

aswell. It's not very convinient to me since you write a "reverse" order someway. Anyway, this syntax gets converted internally into a .find() statement, so you're actually saving a call.

Ref.: .find(), .children()

酒解孤独 2024-10-15 14:13:07

正确的语法是:

$(".child", this)

如果您只想要直接子级:(

$("> .child", this)

归功于Gumbo提到这一点)

更新,两年后

您可以使用 $(this).find('> .child')

The correct syntax is:

$(".child", this)

If you only want the direct children:

$("> .child", this)

(credit goes to Gumbo for mentioning this)

Update, two years later:

You can use $(this).find('> .child')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文