jQuery使用.each具有多个选择器,并知道哪个选择是正确的 [英] jquery using .each with multiple selectors and knowing which one it is currenlty on

查看:98
本文介绍了jQuery使用.each具有多个选择器,并知道哪个选择是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这句话措辞不好.

Sorry, this is poorly worded.

我有一个.each有多个选择器$('.class1,.class2').each(有趣.... 在此示例中,有一种方法可以知道其当前正在.each中迭代哪个类.

I have a .each with multiple selectors $('.class1, .class2').each(fun.... In this example is there a way to know which class it is currently iterating over within the .each.

例如-ILLUSTRATIVE代码

e.g.-ILLUSTRATIVE code

   $('.class1, .class2').each(function () {
        var thisClass = ?$(this).iteratedClassName?;
        if ($(thisClass).prop('checked')) {
            // do something with items with thisClass assigned 
        }
    });

我想通过上面的示例实现的行为是,不同的类名称将每个类的元素组之间的行为彼此独立.

The behavior I am trying to achieve with above example is the different class names group the behavior between each class's group of elements independently of one another.

我可以两次运行.each,但这不是非常灵活或优雅.

I could run the .each twice but that is not very flexible or elegant.

推荐答案

将索引和值作为匿名函数的参数传递

pass index and value as arguments of the anonymous function

$('.class1, .class2').each(function (index, value) {
    console.log(index); //which one in order you're at
    console.log(value); //value you're at
}

这篇关于jQuery使用.each具有多个选择器,并知道哪个选择是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆