慕姐4208626
如果类别(例如:代理、客人)是固定的并且您知道所有可能的组合是什么,您可以像这样进行选择var $main = $('#main'); // filter all the class that are not in the mainvar aClasses = ['agent', 'guest'].filter(function(cl) { return !$main.hasClass(cl)});// build the selectorvar selector = ':not(.hidden)';aClasses.forEach(function(cl) { selector += ':not([data-access="' + cl + '"])'})$('div.area' + selector).each(function(i, el) { $('div.section' + selector, el).each(function(_i, _el) { $('div[data-type]' + selector, _el).each(function(__i, __el) { // you are inside the visible 'div[data-type]' here; do your stuff }); });});或者,像这样一下子做:$( 'div.area' + selector + ' div.section' + selector + ' div[data-type]' + selector).each( function(i, el) { // your stuff}或者,如果您确实不想根据具有类(例如:代理、访客)的主 div 进行选择并检查完全相同的内容,您可以尝试var $main = $('#main'); // get the main div's classvar sClass = (['agent', 'guest'].filter(function(cl) { return $main.hasClass(cl)})[0];// make the two selector combinationvar s1 = ':not(.hidden):not([data-access])'; s2 = '[data-access="' + sClass + '"]:not(.hidden)'; $('div.area' + s1 + ',div.area' + s2).each(function(i, el) { $('div.section' + s1 + ',div.section' + s2, el).each(function(_i, _el) { $('div[data-type]' + s1 + ',div[data-type]' + s2, el).each(function(__i, __el) { // your stuff }); });});但在这里,要一次性写出所有内容,您必须使用 8 种不同的组合例如:// area-s1 sect-s1 div-s1, // area-s1 sect-s1 div-s2, // area-s1 sect-s2 div-s1,// area-s1 sect-s2 div-s2,// area-s2 sect-s1 div-s1,// area-s2 sect-s1 div-s2, // area-s2 sect-s2 div-s1,// area-s2 sect-s2 div-s2,// ie:$( 'div.area' + s1 + ' div.section' + s1 + ' div[data-type]' + s1 + ',div.area' + s1 + ' div.section' + s1 + ' div[data-type]' + s2 + ',div.area' + s1 + ' div.section' + s2 + ' div[data-type]' + s1 + ',div.area' + s1 + ' div.section' + s2 + ' div[data-type]' + s2 + ',div.area' + s2 + ' div.section' + s1 + ' div[data-type]' + s1 + ',div.area' + s2 + ' div.section' + s1 + ' div[data-type]' + s2 + ',div.area' + s2 + ' div.section' + s2 + ' div[data-type]' + s1 + ',div.area' + s2 + ' div.section' + s2 + ' div[data-type]' + s2).each(function(i, el) { // your stuff})所以最好使用嵌套循环本身。例子// assume the classes are (agent, guest) and main div is having class 'agent' then/* First approch */$('div.area:not(.hidden):not([data-access="guest"] div.section:not(.hidden):not([data-access="guest"] div[data-type]:not(.hidden):not([data-access="guest"]').each(function(index, elem) { //your stuff})// using nested loops$('div.area:not(.hidden):not([data-access="guest"]').each(function(i, el) { $('div.section:not(.hidden):not([data-access="guest"'], el).each(function(_i, _el) { $('div[data-type]:not(.hidden):not([data-access="guest"'], _el).each(function(__i, __el) { // you are inside the visible 'div[data-type]' here; do your stuff }); });});/* Second approch */$( 'div.area:not(.hidden):not([data-access]) div.section:not(.hidden):not([data-access]) div[data-type]:not(.hidden):not([data-access]), ' + 'div.area:not(.hidden):not([data-access]) div.section:not(.hidden):not([data-access]) div[data-type][data-access="agent"]:not(.hidden), ' + ...).each(function(i, el) { //your stuff})// using nested loops$('div.area:not(.hidden):not([data-access]), div.area[data-access="agent"]:not(.hidden)').each(function(i, el) { $('div.section:not(.hidden):not([data-access]), div.section[data-access="agent"]:not(.hidden)', el).each(function(_i, _el) { $('div[data-type]:not(.hidden):not([data-access]), div[data-type][data-access="agent"]:not(.hidden)', _el).each(function(__i, __el) { // your stuff }); });});