div.tabs
serves as a container for tab control. Each individual tab is represented by div.tab
, which is a child of div.tabs
. The currently active tab is identified by div.tab.active
.
.data
represents an element that can exist within any div.tab
, or even directly within the body
if there are no .tab
elements present.
Task:
- If there is a
.tabs
element, select all.data
elements that are children of.tab.active
. - If there are no
.tabs
elements, simply select all.data
elements.
I have created two versions to achieve this:
// First version
var data;
if ($('.tabs').length) {
data = $('.tab.active .data');
} else {
data = $('.data');
}
// Section version
var data = $('*');
if ($('.tabs').length) {
data = data.find('.tab.active');
}
data = data.find('.data');
However, I am curious if there exists a function s.findUsingParent(e)
in jQuery that would select elements which are children of e
from a set of elements s
:
// Expect
var data = $('data');
if ($('.tabs').length) {
data = data.findUsingParent('.tab.active');
}
Does such a function already exist in jQuery?
Is there a CSS-only solution to this problem?