且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jQuery从.each循环创建数组

更新时间:2023-01-16 13:01:43

如果你在每个之外声明一个变量,它将是可访问的在每个内:

If you declare a variable outside of the each, it will be accessible inside the each:

var yourArray = [];
$('.profile-nav ul li a').not('.apply').each(function() {
    if($(this).hasClass('cur')) {
        yourArray.push($(this).prop('class').split(' ')[0]);
    }
});
//Here, yourArray will contain the strings you require.

虽然正如其他人所示,但有很多方法可以显着缩短代码。

Although as others have shown, there are ways to shorten your code significantly.