This may seem a little complicated, but I want to find all the <a> containing <img> , so that first the images that are in the same node with the most other images will be selected.
For example, if my page looks like this:
http://img684.imageshack.us/img684/5678/imagechart.gif
If the blue squares are <div> and the pink squares are <img> , then the middle div contains most of the images, then these images are selected first. Since they are not nested anywhere deeper, they simply appear in the order in which they appear on the page. Then the first div is selected (contains the 2nd most of the images), etc ... does that make sense?
We can think of it recursively. First, body is selected, as it will always contain most of the images, then each of the direct children is examined to see what the majority of the descendants of the images (not necessarily direct) contain, then we go to this node and repeat ...
source share