2 回答

TA貢獻1852條經驗 獲得超7個贊
如果類別(例如:代理、客人)是固定的并且您知道所有可能的組合是什么,您可以像這樣進行選擇
var $main = $('#main');
// filter all the class that are not in the main
var aClasses = ['agent', 'guest'].filter(function(cl) {
return !$main.hasClass(cl)
});
// build the selector
var 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 class
var sClass = (['agent', 'guest'].filter(function(cl) {
return $main.hasClass(cl)
})[0];
// make the two selector combination
var 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
})
所以最好使用嵌套循環(huán)本身。
例子
// 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
});
});
});

TA貢獻1802條經驗 獲得超5個贊
jQuery 的:visible偽類將僅過濾[data-type]用戶可見的元素。
因此,根據您的描述,在選擇器上使用它應該足夠了:
<script>
$(function () {
function fetchFormData() {
var result = [];
$('[data-name=holder] [data-type]:visible').each(function (idx, elem) {
var $elem = $(elem);
var type = $elem.data('type');
var label, value;
// specific case: radio input
if (type === 'radio') {
label = $elem.find('legend').text();
value = $elem.find('input[type=radio]:checked').val();
result.push({label: label, value: value});
// done with this item, skip to next one
return true; // continue;
}
// generic case, works with most inputs
label = $elem.find('label').text();
value = $elem.find('input').val();
result.push({label: label, value: value});
});
return result;
}
// add this to an event handler
console.log(fetchFormData());
});
</script>
添加回答
舉報