Jquery / javascript div tag "containment" approach / algorithm?

Reference Information. I created an application for creating online diagrams where div tags are containers containing smaller div containers, etc.

Question. For any particular div tag, I need to quickly determine if it contains other div tags (which, in turn, can contain other div tags).

I searched for jQuery and for this I do not see the built-in procedure. Does anyone know of an algorithm that is faster than O (n ^ 2)?

It looks like I need to go through the list of div tags in the outer loop (n) and have an inner loop (another n) to compare with all other div tags and perform a “leak test” (position, width, height), creating a list of the contained div tags. This is an n-square. Then I need to create a list of all nested div tags, combining the contained lists. Thus, the sum will be equal to O (n ^ 2) + n.

Should there be a better way?

+3
source share
2 answers

You can perform the following checks

$('div_selector').has('div').length > 0 // it contains divs

as an alternative

$('div_selector').is(':has(div)') // returns true if it contains divs

the above checks work for actual containment (nesting) in dom (not for visual localization where the dimensions of one div are contained in another).

+1
source

jQuery. , "div" - :

if (div.getElementsByTagName('div').length > 0) {
  // do something
}
+1

Source: https://habr.com/ru/post/1748068/


All Articles