This example returns an Array of the text nodes contained by node and its descendents.
It skips nodes containing only white space
function deepText(node){
var A= [], tem;
if(node){
node= node.firstChild;
while(node!= null){
if(node.nodeType== 3){
if(/S/.test(node.data)) A[A.length]= node;
}
else A= A.concat(deepText(node));
node= node.nextSibling;
}
}
return A;
}
//test
var text= [], nodes= deepText(document.getElementById('product-1'));
for(var i= 0, L= nodes.length; i<L; i++){
// do whatever you want to the node
var pa= nodes[i].parentNode;
if(pa.nodeName== 'H4') pa.style.color= 'blue';
text.push(nodes[i].data);
}
alert(text.join(' '))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…