Consider the following markup:
<div>Hello, my <span>name</span> is John</div>
I need to get the text nodes and the elements containing them, sequentially, as in:
1: "Hello, my "
, <div>
(HTMLElement)
2: "name"
, <span>
(HTMLElement)
3: " is John"
, <div>
(HTMLElement)
This needs to be done in a way that allows me to get CSS style of HTMLElements later.
What I already tried:
$(foo).find('*').contents().filter(function() {
var $this = $(this);
return this.nodeType === 3 && $.trim($this.text()).length > 0;
});
This results in a non-sequential result set, as in:
1: "Hello, my "
2: " is John"
3: "name"
However, I can access their parent elements, so this does half of the job.
The main question, therefore, is: how do I get text nodes in the same sequence they are in the document?